Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Benchmarking - CPU time bigger than wall time?

I measure cpu time and wall time of sorting algorithms on linux. Im using getrusage to measure a cpu time and clock_gettime CLOCK_MONOTONIC to get a wall time. Althought I noticed that a cpu time is bigger than wall time - is that correct? I always thought that cpu time MUST be less than wall time. My example results:

3.000187 seconds  [CPU]
3.000001 seconds  [WALL]
like image 324
mazix Avatar asked Jul 24 '13 19:07

mazix


Video Answer


1 Answers

If a computation requires two seconds of processor time, then two processors can (ideally) complete it in one second. Hence a two-processor system has two CPU seconds for every wall-clock second. Even if you do not use multi-threading explicitly in your process, a library you use or the operating system may use multiple processors to perform work for your process.

Additionally, some of the accounting is approximate. A system might track processor time in some small unit, say microseconds for the purpose of argument, and charge a process for a microsecond anytime the process receives at least half a microsecond of processor time. (Which should be a lesson to all the people who answer floating-point questions with recommendations to use integer arithmetic to avoid rounding errors. All discrete arithmetic can have rounding errors.)

like image 87
Eric Postpischil Avatar answered Sep 21 '22 06:09

Eric Postpischil