Created
January 27, 2015 00:31
-
-
Save afrachioni/f441de4ab75b8c7de264 to your computer and use it in GitHub Desktop.
Example of timing using gettimeofday
This file contains 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 <sys/time.h> | |
#include <stdio.h> | |
unsigned long get_time(); | |
int main(int nargs, char **args) { | |
fprintf (stdout, "The current epoch time / ms: %ld\n", get_time()); | |
long start_time = get_time(); | |
int i; | |
for (i = 0; i < 1e8; ++i); | |
fprintf (stdout, "It took %ld ms to count to 10^8.\n", \ | |
get_time() - start_time); | |
return 0; | |
} | |
unsigned long get_time() { | |
struct timeval tv; | |
gettimeofday(&tv, NULL); | |
unsigned long ret = tv.tv_usec; | |
ret /= 1000; | |
ret += (tv.tv_sec * 1000); | |
return ret; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment