11 December, 2009

The fastest way to get time in millisecond on linux

"gettimeofday()" is a great function to get time in millisecond.

Sample usage:
struct timeval start, end;
gettimeofday(&start, NULL);
usleep(2000);
gettimeofday(&end, NULL);

printf("%ld milliseconds\n", ((end.tv_sec-start.tv_sec) * 1000 + (end.tv_usec-start.tv_usec)/1000.0));

Moreover, we can even speed up this function!
According to this post, we can enable Virtual Dynamic Shared Object to speed things up.
# echo 2 > /proc/sys/kernel/vsyscall64


1 comment:

Hai-Ning Wu said...

這讓我想到當年在uiuc搞embedded lab的時光 XD