Skip to content

Instantly share code, notes, and snippets.

@jarun
Created July 5, 2017 18:39
Show Gist options
  • Save jarun/42c31f718b6299c4a4996cf11d490c0c to your computer and use it in GitHub Desktop.
Save jarun/42c31f718b6299c4a4996cf11d490c0c to your computer and use it in GitHub Desktop.
microsec timer in C for profiling
#include <iostream>
#include <sys/time.h>
int main()
{
struct timeval t1, t2;
double elapsedTime;
// start timer
gettimeofday(&t1, NULL);
// do something
// ...
// stop timer
gettimeofday(&t2, NULL);
// compute and print the elapsed time in millisec
elapsedTime = (t2.tv_sec - t1.tv_sec) * 1000.0; // sec to ms
elapsedTime += (t2.tv_usec - t1.tv_usec) / 1000.0; // us to ms
printf("%lf\n", elapsedTime);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment