Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _GNU_SOURCE
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <sys/ioctl.h>
- #include <sys/time.h>
- #include <sys/select.h>
- #include <termios.h>
- static struct termios orig_termios;
- void tty_raw(void)
- {
- struct termios raw;
- raw = orig_termios;
- raw.c_lflag &= ~(ECHO | ICANON);
- raw.c_cc[VMIN] = 1; raw.c_cc[VTIME] = 0;
- if (tcsetattr(0, TCSAFLUSH, &raw) < 0) exit(1);
- }
- void tty_reset(void)
- {
- tcsetattr(0, TCSAFLUSH, &orig_termios);
- }
- char str[2000] = "abcdefghijklmnopqrstuvwxyz 0123456789 ~!@#$%^&*():";
- char buf[64];
- void usage(void)
- {
- puts("");
- puts("usage: hold enter until you get the results back (it should take less than 10 seconds)");
- puts("recommendation: run 'xset r rate 300 40' before starting in order to compare with my results");
- puts("");
- puts("press and hold enter to start");
- read(1, buf, 64);
- read(1, buf, 64);
- }
- int rows;
- int get_micros_in_x(void)
- {
- int i;
- struct timeval start, end, delta;
- int micros = 0;
- for (i = 0; i < 3*rows; ++i) {
- // Not an exact science but seems to correlate a tiny bit
- gettimeofday(&start, NULL);
- read(1, buf, 64);
- gettimeofday(&end, NULL);
- timersub(&end, &start, &delta);
- micros += delta.tv_sec*1000000 + delta.tv_usec;
- if (buf[0] == 27) {
- exit(0);
- }
- printf(" %s\r\n", strfry(str));
- // This shouldn't matter, right? RIGHT???
- usleep(40000);
- }
- return micros;
- }
- int main(void)
- {
- int i;
- struct winsize w;
- struct timeval tv;
- fd_set set;
- if (!isatty(0)) exit(1);
- if (tcgetattr(0, &orig_termios) < 0) exit(3);
- tty_raw();
- atexit(tty_reset);
- ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
- rows = w.ws_row;
- while (strlen(str) < 1000) memcpy(str+strlen(str), str, strlen(str));
- str[w.ws_col - 2] = 0;
- usage();
- write(1, "\33[H\33[2J", 7);
- int slow = get_micros_in_x();
- str[0] = '.';
- str[1] = 0;
- write(1, "\33[H\33[2J", 7);
- int fast = get_micros_in_x();
- printf("%3d.%06d seconds have elapsed in the printing setup\n", slow/1000000, slow%1000000);
- printf("%3d.%06d seconds have elapsed in the empty setup\n", fast/1000000, fast%1000000);
- puts("you can release enter now");
- // wait until the user releases enter
- FD_ZERO(&set);
- FD_SET(0, &set);
- tv.tv_sec = 0;
- tv.tv_usec = 250000;
- while (select(1, &set, NULL, NULL, &tv) == 1) {
- read(1, buf, 64);
- tv.tv_sec = 0;
- tv.tv_usec = 250000;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment