Reputation: 4137
I used to take clock()
to get CPU time of my algorithms. However, it does not seem to work anymore. I have a Windows 10 VM with 8 CPUs as also seen in the resource monitor.
Now, I measure time like this:
auto startTime = std::chrono::high_resolution_clock::now();
auto startClocks = std::clock();
// some code with TBB that uses multiple threads
auto endTime = std::chrono::high_resolution_clock::now();
auto endClocks = std::clock();
auto duration = endTime - startTime;
auto clockDuration = endClocks - startClocks;
auto durationSeconds = static_cast<double>(std::chrono::duration_cast<std::chrono::milliseconds>(duration).count()) / 1000.;
auto durationCpuSeconds = 1. * clockDuration / CLOCKS_PER_SEC;
The TBB part surely works, as I see in the resource monitor of Windows that all CPUs work with 100%. If I start some endless loop without parallelization, CPU usage is only 12.5% as expected.
However, durationSeconds
and durationCpuSeconds
are exactly the same...
I have measured the time with my watch and the result is the wall time. So, clock()
clearly does not show the CPU time which should be substantially higher with 8 CPUs working 100% in parallel. Is clock()
not reliable or am I missing something here?
Upvotes: 3
Views: 91
Reputation: 385144
Yeah, it's broken on Windows.
The clock function tells how much wall-clock time has passed since the CRT initialization during process start. Note that this function does not strictly conform to ISO C, which specifies net CPU time as the return value. To obtain CPU times, use the Win32 GetProcessTimes function.
(from Microsoft's clock
docs)
Upvotes: 6