【问题标题】:Python CPU time on windowsWindows上的Python CPU时间
【发布时间】:2017-04-09 07:00:18
【问题描述】:

我正在寻找一种方法来测量我的 python 程序部分的 CPU 时间。我环顾四周,似乎一直在测量我能找到的测量窗户上挂钟时间的功能。有没有办法在 windows 上为 python 测量 CPU 时间?谢谢。

【问题讨论】:

    标签: python python-3.x time


    【解决方案1】:

    time.process_time()

    https://www.python.org/dev/peps/pep-0418/

    新的 time.process_time() 函数充当便携式计数器, 总是测量 CPU 时间(不包括睡眠期间经过的时间)并且有 最佳可用分辨率。

    较早的答案适用于任何想要使用 timeit() 来平均的人:

    我最近在学习 Python 时写了这篇文章。这是一个 timeit.Timer 类迭代 timeit() 函数进行平均;在我的研究中,时间似乎很容易搞砸,而 timeit() 显然很难开始工作(尽管更容易在命令行上工作)。这行得通。

    另请注意,此答案表明对于 Python 3.3+,最好的选择是 time.process_time(),并提醒 .default_timer() 是挂钟时间:

    https://stackoverflow.com/a/15176730/3981745

    """ Sum a range with timing/comparisons.
    
        - Manually summing from low..high : a beginner's loop with no algorithms experience
        - Python's sum() - does the same thing for an "efficient" programmer
        - Fast sum - a programmer with math focus 
        - Timing calls (easy to do wrong)
    
        This is a trivial formula to demonstrate what a little math can do; for games this type of optimization is critical. It could probably be even more optimized.
    """
    
    def slow_way(lo, hi):
        s=0
        for i in range(lo, hi+1):
            s+=i
        return s
    
    def fast_way(lo, hi):
        lph=lo+hi        # lo plus hi
        hmlpo=hi-lo+1    # hi minus lo plus one
        pairs=hmlpo//2
    
        #print("-", lo,hi,pairs, pairs*lph, (lo+pairs)*(hmlpo%2))
    
        return (pairs*lph + (lo+pairs)*(hmlpo%2))
    
    import timeit
    # 'builtin' hack doesn't seem to work
    #import __builtin__
    #__builtin__.__dict__.update(locals()) 
    
    ranges=[]
    ranges.append((1,10,))
    ranges.append((2,10,))
    ranges.append((3,10,))
    ranges.append((4,10,))
    ranges.append((5,10,))
    ranges.append((32,10032,))
    
    print("Calculation check...")
    print("slow : sum : fast : should all match")
    for tupl in ranges:
        l=tupl[0]; h=tupl[1]
        print("{} : {} : {}".format(slow_way(l,h), sum(range(l, h+1)), fast_way(l,h)))
    
    iters=100000
    print("-"*20 +"\nTime compare ({} iterations) : lower is better".format(iters))
    slow=timeit.Timer("slow_way(1,101)", "from __main__ import slow_way")
    print("slow: {0:.6f}".format(slow.timeit(number=iters)))
    
    # functions include last number, range should be +1
    s=timeit.Timer("sum(range(1,102))", "")
    print(" sum: {0:.6f}".format(s.timeit(number=iters)))
    
    fast=timeit.Timer("fast_way(1,101)", "from __main__ import fast_way")
    print("  fast: {0:.6f}".format(fast.timeit(number=iters)))
    

    输出

    Calculation check...
    slow : sum : fast : should all match
    55 : 55 : 55
    54 : 54 : 54
    52 : 52 : 52
    49 : 49 : 49
    45 : 45 : 45
    50325032 : 50325032 : 50325032
    --------------------
    Time compare (100000 iterations) : lower is better
    slow: 4.719885
     sum: 0.963886
      fast: 0.343524
    

    【讨论】:

    • 我不介意你的编辑......或者如果人们认为这样更好,我可以自己删除无关紧要的内容。
    【解决方案2】:

    time.clock()

    在 Windows 上,您可以使用 time.clock(),它基本上使用来自 Windows API 的 QueryPerformanceCounter()

    查询性能计数器():

    检索性能计数器的当前值,这是一个高分辨率 (

    这里是来自 python 文档的 time.clock 的详细解释:

    在 Unix 上,将当前处理器时间作为浮点数返回,以秒为单位。精度,实际上是“处理器时间”含义的定义,取决于同名 C 函数的精度,但无论如何,这是用于对 Python 或计时算法进行基准测试的函数。 在 Windows 上,此函数根据 Win32 函数 QueryPerformanceCounter() 返回自第一次调用此函数以来经过的挂钟秒数,作为浮点数。分辨率通常优于 1 微秒。

    时间

    如果您需要更准确的信息,请访问 timeit。这个答案解释了timeit的好处:https://stackoverflow.com/a/17579466/2394967

    【讨论】:

    • 这就是问题所在。我目前正在使用带有此功能的 timeit。 start_time = timeit.default_timer() 已过 = (timeit.default_timer() - start_time)。但是当我在这些之间设置暂停时,它仍然会计算暂停时间。这不是让它成为挂钟计时器而不是 CPU 计时器吗?
    • 但是 time.clock() 也给了我一个挂钟,而且似乎只有在 Unix 上使用它才会给 CPU 时间。我如何让它在 Windows 上给我 CPU 时间:)
    • 验证时间它的时钟选择...同时docs.python.org/3/library/time.html : process_time()
    • 好的,process_time() 似乎给出了想要的结果。文档指出它给出了系统和用户 CPU 时间的总和,这对我有用。谢谢。
    • QueryPerformanceCounter 又是一个高分辨率的墙上时钟
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-13
    • 1970-01-01
    • 2013-06-30
    • 2013-06-03
    • 2013-02-17
    • 2014-08-03
    • 1970-01-01
    相关资源
    最近更新 更多