代码之家  ›  专栏  ›  技术社区  ›  Eric Wilson

如何在Python中确定处理时间?

  •  60
  • Eric Wilson  · 技术社区  · 16 年前

    在java中,我会写:

    long timeBefore = System.currentTimeMillis();
    doStuff();
    long timeAfter = System.currentTimeMillis();
    elapsed time = timeAfter - timeBefore;
    

    9 回复  |  直到 16 年前
        1
  •  95
  •   sanyassh Khushboo Tahir    6 年前

    >>> import time
    >>> tic = time.clock()
    >>> toc = time.clock()
    >>> toc - tic
    

    timeit .

        2
  •  39
  •   Bryan P    12 年前

    基于并更新一些早期的响应(感谢:SilentGhost、nosklo、Ramkumar),一个简单的便携式计时器将使用 timeit default_timer() :

    >>> import timeit
    >>> tic=timeit.default_timer()
    >>> # Do Stuff
    >>> toc=timeit.default_timer()
    >>> toc - tic #elapsed time in seconds
    

    这将返回经过的挂钟(实时)时间,而不是CPU时间。如所述 timeit documentation 根据平台选择最精确的可用真实世界计时器。

    time.perf_counter 性能计数器。在3.3+timeit.default_timer()下引用这个新计数器。

    时间 包括对小代码片段自动计时的更复杂的调用,包括在定义的重复集上平均运行时间。

        3
  •  15
  •   user263387    9 年前

    tic() tac() 功能,其中 tic() tic() 被召唤。以下是一个简短的实现:

    import time
    
    _start_time = time.time()
    
    def tic():
        global _start_time 
        _start_time = time.time()
    
    def tac():
        t_sec = round(time.time() - _start_time)
        (t_min, t_sec) = divmod(t_sec,60)
        (t_hour,t_min) = divmod(t_min,60) 
        print('Time passed: {}hour:{}min:{}sec'.format(t_hour,t_min,t_sec))
    

    tic()
    do_some_stuff()
    tac()
    

    例如,它将输出:

    Time passed: 0hour:7min:26sec
    

        4
  •  15
  •   Michael Dorner Satyanarayana    7 年前

    time.process_time()

    import time
    
    t = time.process_time()
    #do some stuff
    elapsed_time = time.process_time() - t
    
        5
  •  6
  •   artagnon    16 年前
        6
  •  2
  •   Kushan Gunasekera    4 年前

    我还需要计算一些代码行的处理时间。所以我尝试了批准的答案,并得到了这个警告。

    DeprecationWarning: time.clock has been deprecated in Python 3.3 and will be removed from Python 3.8: use time.perf_counter or time.process_time instead
    

    remove time.clock() Python 3.8 #13270 time.clock() 在文档中,还详细提及了这一警告 time.chock()

    让我们详细看看这两个函数。


    返回性能计数器的值(以分数秒为单位),即具有最高可用分辨率的时钟,用于测量短持续时间。它确实包括睡眠期间经过的时间,并且是全系统的。返回值的引用点未定义,因此只有两次调用结果之间的差异有效。

    3.3版本中的新功能。

    在3.10版本中更改:在Windows上,该功能现在是系统范围的。

    所以,如果你想 nanoseconds time.perf_counter_ns() time.sleep(secs)

    import time
    
    
    def func(x):
        time.sleep(5)
        return x * x
    
    
    lst = [1, 2, 3]
    tic = time.perf_counter()
    print([func(x) for x in lst])
    toc = time.perf_counter()
    print(toc - tic)
    
    # [1, 4, 9]
    # 15.0041916 --> output including 5 seconds sleep time
    

    返回当前进程的系统和用户CPU时间之和的值(以秒为单位)。它不包括睡眠期间经过的时间。从定义上讲,它是全过程的。返回值的引用点未定义,因此只有两次调用结果之间的差异有效。

    3.3版本中的新功能。

    所以,如果你想 ,您可以使用 time.process_time_ns()

    import time
    
    
    def func(x):
        time.sleep(5)
        return x * x
    
    
    lst = [1, 2, 3]
    tic = time.process_time()
    print([func(x) for x in lst])
    toc = time.process_time()
    print(toc - tic)
    
    # [1, 4, 9]
    # 0.0 --> output excluding 5 seconds sleep time
    

    请注意两者 time.perf_counter_ns() time.process_time_ns() 想出 Python 3.7

        7
  •  1
  •   pixelbeat    16 年前
    python -m timeit -h
    
        8
  •  1
  •   Community Mohan Dere    8 年前

    start = time.time()
    

    as of 3.3, time.clock() is deprecated

    start = time.clock()
    

    Python - time.clock() vs. time.time() - accuracy?

    start = time.time()
    
    ... do something
    
    elapsed = (time.time() - start)
    
        9
  •  1
  •   tyleha    7 年前

    tic() toc()

    tic()
    
    ''' some code that runs for an interesting amount of time '''
    
    toc()
    
    # OUTPUT:
    # Elapsed time is: 32.42123 seconds
    

    超级,非常容易使用,有点像即发即弃代码。它可以在Github的Gist上找到 https://gist.github.com/tyleha/5174230