为什么time.clock给出的经过时间比time.time更长? [英] Why is time.clock giving a greater elapsed time than time.time?

查看:241
本文介绍了为什么time.clock给出的经过时间比time.time更长?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Ubuntu上使用time.clocktime.time设置了一段python代码的时间:

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

我知道time.time使用系统时间,而time.clock使用处理器时钟.当time.time提供的经过时间比time.clock更长的时间时,这对我来说很有意义:处理器在整个时间(例如,调用time.sleep的时间)都没有处于活动状态.

但是为什么/何时处理器时钟给出的经过时间比系统时间大 呢?


附录

我做了一个粗略的测试,用一个标准的映射,一个进程池的映射和一个线程池的映射来计算相同的功能.可以理解,进程池更快,线程池更慢.更有趣的是:时钟时间比处理器池中的时间更短,但线程池中的时间更大.

同样,我理解为什么处理器池的时钟时序会更少:大概主进程并没有做太多事情,只是等待池进程完成.但是,为什么时钟定时与线程池有关呢?有什么见识吗?

结果:

map
  time  1738.8
  clock 1739.6
mp pool
  time   580.1
  clock   15.9
thread pool
  time  3455.3
  clock 5378.9

代码:

from time import clock, sleep, time
from multiprocessing.pool import ThreadPool
from multiprocessing import Pool
import random

def f(i):
    x = [random.random() for j in range(100000)]
    return x[i]

def t(fn):
    t0, c0 = time(), clock()
    for i in range(10): fn(f,range(16))
    print '  time ', round(1000*(time()-t0),1)
    print '  clock', round(1000*(clock()-c0),1)

if __name__ == '__main__':
    print 'map'
    t(map)

    pool = Pool(8)
    print 'mp pool'
    t(pool.map)

    pool = ThreadPool(8)
    print 'thread pool'
    t(pool.map)

解决方案

如果您在多个CPU上执行,则CPU时间可能会超过墙壁时间.我在Python中还没有特别看到它,但是当clock函数与C中的多个线程一起使用时,我肯定已经看到了这一点,并且大概Python代码只是直接调用此C函数.

关于为什么":您正在以错误的方式思考它.重要的是有多少个内核正在运行您的程序.如果一个内核在两秒钟的时间间隔内运行一秒钟,这对您来说很有意义,但是如果四个内核在相同的时间间隔内运行一秒钟怎么办?那么您就有4秒的CPU时间和2秒的固定时间.内核负责测量所有内核的CPU时间.如果多个内核在同一秒内运行,则您在该秒内花费了多个CPU秒.这是对计划程序重要的成本度量,并且大概是clock建立的度量标准.这可能不是您关心的指标,但这就是它的工作原理.

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

I understand that time.time uses system time and time.clock uses the processor clock. It makes sense to me when time.time gives a larger elapsed time than time.clock: the processor was just not active the entire time (e.g. time a call to time.sleep).

But why/when would the processor clock give an elapsed time so much greater than the system time does?


addendum

I did a rough test computing the same function with a standard map, with a process pool map, and a thread pool map. Understandably, the process pool is faster and the thread pool is slower. More interestingly: the clock timing is less than the time timing with the processor pool but greater in the thread pool.

Again, I understand why the clock timing is less with the processor pool: presumably the master process doesn't do much and just waits for the pool processes to complete. But why is the clock timing more with the thread pool? Any insights?

Results:

map
  time  1738.8
  clock 1739.6
mp pool
  time   580.1
  clock   15.9
thread pool
  time  3455.3
  clock 5378.9

Code:

from time import clock, sleep, time
from multiprocessing.pool import ThreadPool
from multiprocessing import Pool
import random

def f(i):
    x = [random.random() for j in range(100000)]
    return x[i]

def t(fn):
    t0, c0 = time(), clock()
    for i in range(10): fn(f,range(16))
    print '  time ', round(1000*(time()-t0),1)
    print '  clock', round(1000*(clock()-c0),1)

if __name__ == '__main__':
    print 'map'
    t(map)

    pool = Pool(8)
    print 'mp pool'
    t(pool.map)

    pool = ThreadPool(8)
    print 'thread pool'
    t(pool.map)

解决方案

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.

这篇关于为什么time.clock给出的经过时间比time.time更长?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆