Skip to content

Instantly share code, notes, and snippets.

@EvanTheB
Created January 29, 2018 01:47
Show Gist options
  • Save EvanTheB/330b187bdbbd35840117da81378f44f4 to your computer and use it in GitHub Desktop.
Save EvanTheB/330b187bdbbd35840117da81378f44f4 to your computer and use it in GitHub Desktop.
helper macro for timing a function in c - beware of optimisation
#define TIMEIT(F, N, REPS) \
{\
clock_t start = clock();\
for (int i = 0; i < N; ++i)\
{\
}\
clock_t diff_loop = clock() - start;\
double results[REPS];\
for (int i = 0; i < REPS; ++i)\
{\
clock_t start = clock();\
for (int j = 0; j < N; ++j)\
{\
F;\
}\
clock_t diff = clock() - start - diff_loop;\
results[i] = ((double)diff) / CLOCKS_PER_SEC;\
}\
res = meana(results, REPS);\
if (maxa(results, REPS) - mina(results, REPS) > 0.1 * res)\
{\
fprintf(stderr, "spread large\n");\
}\
}\
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment