Why is time.clock giving a greater elapsed time than time.time?

后端 未结 1 1372
余生分开走
余生分开走 2021-01-18 04:37

I timed a section of python code on Ubuntu using time.clock and time.time:

clock elapsed time: 8.770 s
time  elapsed time: 1.869 s
         


        
相关标签:
1条回答
  • 2021-01-18 05:28

    CPU time can exceed wall time if you execute on multiple CPUs. I've not specifically seen this in Python, but I've definitely seen this when using the clock function with multiple threads from C, and presumably the Python code is just directly calling this C function.

    Regarding "why": you're thinking about it the wrong way. What's important is how many cores are running your program. If one core runs for one second over the course of two seconds of wall time that makes sense to you, but what if four cores each run for one second over that same time interval. Then you have 4 seconds of CPU time in 2 seconds of wall time. The kernel accounts for CPU time measuring all cores. If multiple cores run during the same second then you spent multiple CPU seconds during that second. That's the cost measurement that matters to the scheduler, and presumably that's the metric which clock is built upon. This may not be the metric you care about, but that's how it works.

    0 讨论(0)
提交回复
热议问题