I am currently using time from the ctime library. Is there a faster alternative?
time_t start_time, elapsed_time; for(int i = 0; i < n; i++) { start_time = time(NULL); /// optimized code if(condition_met()) { elapsed_time = time(NULL) - start_time; } else continue; }
time (NULL) is just not fast enough.
It seems you only want to measure the elapsed time (and are not related to absolute time). One of the fastest ways to measure elapsed time (if you are on x86) is to read the rdtsc counter . In mvsC ++, this can be achieved:
#include <intrin.h> unsigned __int64 rdtsc(void) { return __rdtsc(); }
I'm not sure, but I assume that given that it counts whole seconds, you can say that it is time(NULL)not granular enough. In other words, you may want to go down to milli, micro or nano seconds.
time(NULL)