Skip to content

Instantly share code, notes, and snippets.

@nick87720z
Last active June 15, 2020 15:06
Show Gist options
  • Save nick87720z/5b84e7a09a3df7c85bf12b29f2606a56 to your computer and use it in GitHub Desktop.
Save nick87720z/5b84e7a09a3df7c85bf12b29f2606a56 to your computer and use it in GitHub Desktop.
Inline counter wrapper
#include <time.h>
#include <stdio.h>
/* In-place counter wrapper code */
#define begin_perf(time, idle_time, name) { \
clock_t tstart = clock (); \
clock_t tcur = 0; \
guint64 counter = 0; \
int sec = -(idle_time); \
putchar ('\n'); \
while (1) \
{ \
counter++; \
tcur = clock (); \
if (time == -1) \
{ \
printf ("clock doesn't work\n"); \
break; \
} \
if ( tcur - tstart >= CLOCKS_PER_SEC ) \
{ \
sec++; \
tstart += CLOCKS_PER_SEC; \
if (sec <= 0) \
{ \
counter = 0; \
continue; \
} \
printf ("\r%s: %lu loops ", (name), counter / sec); \
fflush (stdout); \
if (sec >= (time)) \
break; \
}
#define end_perf \
} \
putchar ('\n'); \
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment