Created
April 27, 2013 15:13
-
-
Save chiral/5473438 to your computer and use it in GitHub Desktop.
libev periodic timer resolution test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <sys/time.h> | |
| #include <ev.h> | |
| const int log_size = 10000; | |
| double log[log_size]; | |
| int log_index=0; | |
| double now_usec() { | |
| timeval tv; | |
| gettimeofday(&tv,NULL); | |
| double ret = (double)(1000000.0*tv.tv_sec+tv.tv_usec); | |
| return ret; | |
| } | |
| void clock_cb(EV_P_ ev_periodic *w, int re) { | |
| double now = now_usec(); | |
| log[log_index++] = now; | |
| if (now > log[0]+2000000.0 || | |
| log_index>=log_size) { | |
| ev_unloop(EV_A_ EVUNLOOP_ALL); | |
| } | |
| } | |
| int main(int argc, char *argv[]) { | |
| log_index=0; | |
| log[log_index++] = now_usec(); | |
| struct ev_loop *loop = ev_default_loop(0); | |
| ev_periodic tick; | |
| ev_periodic_init(&tick, clock_cb, 0., 0.001, 0); | |
| ev_periodic_start(loop, &tick); | |
| ev_loop(loop, 0); | |
| for (int i=0; i<log_index; i++) { | |
| printf("[%d] = %lf\n",i,log[i]/1000000.0); | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment