3

I'm trying to do some performance measures in c++ by measuring the real elapsed time in milliseconds vs. the cpu time in milliseconds. This is how my code looks like:

auto start = std::chrono::high_resolution_clock::now();
unsigned begin = clock();

// some computationally expensive task

auto finish = std::chrono::high_resolution_clock::now();
unsigned end = clock();

(finish - start).count();

int duration = std::chrono::duration_cast<std::chrono::milliseconds>(finish - start).count();
int cpu_duration = 1000*(end - begin)/(CLOCKS_PER_SEC);

Now I would expect the cpu time value to be lower than the system time, because the thread might be interrupted. However, the cpu time is 2-3 times higher than the real time. Am I doing something wrong or am I misunderstanding the concept of cpu time?

Ben
  • 4,486
  • 6
  • 33
  • 48
  • How exactly? Should I divide the cpu time by the amount of cpu's? The task is single-threaded. – Ben Jun 04 '19 at 13:47
  • why divide? If you want to know how much cpu time you used then it is the cpu time (no division) – 463035818_is_not_an_ai Jun 04 '19 at 13:49
  • So this means my task is running on more than one core simultaneously? – Ben Jun 04 '19 at 13:53
  • clock() can be misleading, it doesn't actually measure how long the posted code took. On a *nix system it includes the time taken by any other worker threads, even if they had nothing whatsoever to do with // some computationally expensive task – Hans Passant Jun 04 '19 at 13:54
  • @Ben Not necessarily your task. The process in general seems to be using more than one thread. – François Andrieux Jun 04 '19 at 13:54
  • So there is no way to find out the cpu time for a specific thread? – Ben Jun 04 '19 at 13:56
  • @Ben I don't believe so, at least not in a portable way. Your platform's API may very well offer a function for that. – François Andrieux Jun 04 '19 at 13:59
  • I think more accurately: There's no way for us to confirm that your "some computationally expensive task" hasn't been multi-threaded without your knowledge under the hood. – Howard Hinnant Jun 04 '19 at 14:00
  • @Howard Hinnant: I basically iterate over std::vectors and increment counters. Nothing fancy. So I don't think there is multi-threading without my knowledge under the hood. That's why I was surprised. I do have multiple worker threads though. – Ben Jun 04 '19 at 14:10
  • I could not replicate your symptoms on my platform without launching some threads. – Howard Hinnant Jun 04 '19 at 14:11

1 Answers1

6

In a nutshell:

  • real-time: time as measured with a normal clock on your wall
  • cpu-time: total time the CPU(s) was/were busy

If you have more than a single cpu then their times add up, such that eg in 1 second real-time you can use 4 seconds cpu time.

cppreference's documentation on std::clock explicitly distinguishes between wall clock and cpu time :

[...]if the CPU is shared by other processes, std::clock time may advance slower than wall clock. On the other hand, if the current process is multithreaded and more than one execution core is available, std::clock time may advance faster than wall clock.

For more detials see eg here.

François Andrieux
  • 28,148
  • 6
  • 56
  • 87
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • So is there a way to measure just the OP's "some computationally expensive task" section (assuming it is single-threaded)? He is trying to measure his code and not the system's overhead. Or is this overhead repeatable and related only to his app (i.e., no spurious interrupts for other unrelated tasks), so arguably should be included in the computation. – Cosmo Jul 31 '19 at 15:28