Skip to content

Instantly share code, notes, and snippets.

@chiral
Created April 27, 2013 15:13
Show Gist options
  • Select an option

  • Save chiral/5473438 to your computer and use it in GitHub Desktop.

Select an option

Save chiral/5473438 to your computer and use it in GitHub Desktop.
libev periodic timer resolution test
#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