Fast time function C/C++

c, c++, performance, time

Solution

You seem to want to only measure elapsed time (and aren't concerned with the absolute time). One of the fastest approaches of measuring elapsed time (if you are on x86) is to read the rdtsc counter. In mvsc++ this can be achieved by:

#include <intrin.h>
unsigned __int64 rdtsc(void)
{
    return __rdtsc();
}

Problem

I am currently using time from the ctime library. Is there any 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) just isn't fast enough.

Original source

Related problems