I have to compute execution time of a C++ code snippet in seconds. It must be working either on Windows or Unix machines.
I use code the following code to do this. (import before)
clock_t startTime = clock();
// some code here
// to compute its execution duration in runtime
cout << double( clock() - startTime ) / (double)CLOCKS_PER_SEC<< " seconds." << endl;
However for small inputs or short statements such as a = a + 1, I get "0 seconds" result. I think it must be something like 0.0000001 seconds or something like that.
I remember that System.nanoTime()
in Java works pretty well in this case. However I can't get same exact functionality from clock()
function of C++.
Do you have a solution?
It is better to run the inner loop several times with the performance timing only once and average by dividing inner loop repetitions than to run the whole thing (loop + performance timing) several times and average. This will reduce the overhead of the performance timing code vs your actual profiled section.
Wrap your timer calls for the appropriate system. For Windows, QueryPerformanceCounter is pretty fast and "safe" to use.
You can use "rdtsc" on any modern X86 PC as well but there may be issues on some multicore machines (core hopping may change timer) or if you have speed-step of some sort turned on.
I created a lambda that calls you function call N times and returns you the average.
You can find the c++11 header here.
If you want to get good exact results, then as stated above, your execution time does depend on thread scheduling. A complete unfailing solution to this, that should yield exactly the same times per each test, is to compile your program to be OS independent and boot up your computer so as to run the program in an OS-free environment. However, A good substitute to this is just to set the affinity of the current thread to 1 core and the priority to the highest. The result of doing this is very consistent results. And, one more thing is that you should turn off optimizations, which for g++ or gcc means adding -O0 to the command line, to prevent your code being tested from being optimized out. Here is an example of how I am bench-marking square root functions on a Windows computer.
Also, credit to Mike Jarvis for his Timer.
And, please note (this is very important) that if you are going to be running bigger code snippets, then you REALLY must turn down the number of iterations to prevent your computer from freezing up.
I have another working example that uses microseconds (UNIX, POSIX, etc).
Here's the file where we coded this:
https://github.com/arhuaco/junkcode/blob/master/emqbit-bench/bench.c