Skip to content

Instantly share code, notes, and snippets.

@arakashic
Last active May 26, 2024 22:41
Show Gist options
  • Save arakashic/c54821d4ef91ccc9ac098c5d0ce42461 to your computer and use it in GitHub Desktop.
Save arakashic/c54821d4ef91ccc9ac098c5d0ce42461 to your computer and use it in GitHub Desktop.
Quick Snip for Time Measurement
#include <time.h>
double TL_gettime();
int TL_checktick();
double TL_gettime()
{
struct timespec t;
clock_gettime(CLOCK_REALTIME, &t);
return ( (double) t.tv_sec + (double) t.tv_nsec * 1.e-9 );
}
int TL_checktick()
{
int i, minDelta, Delta;
double t1, t2, timesfound[M];
int M = 20;
/* Collect a sequence of M unique time values from the system. */
for (i = 0; i < M; i++) {
t1 = TL_gettime();
while(((t2 = TL_gettime()) - t1) < 1.0E-6 );
timesfound[i] = t1 = t2;
}
/*
* Determine the minimum difference between these M values.
* This result will be our estimate (in microseconds) for the
* clock granularity.
*/
minDelta = 1000000;
for (i = 1; i < M; i++) {
Delta = (int) (1.0E6 * (timesfound[i] - timesfound[i-1]));
minDelta = MIN(minDelta, MAX(Delta, 0));
}
return(minDelta);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment