我想测量执行一个函数所花费的时间。我没时间工作:

import timeit
start = timeit.timeit()
print("hello")
end = timeit.timeit()
print(end - start)

当前回答

给定要计时的函数,

测试.py:

def foo(): 
    # print "hello"   
    return "hello"

使用timeit的最简单方法是从命令行调用它:

% python -mtimeit -s'import test' 'test.foo()'
1000000 loops, best of 3: 0.254 usec per loop

不要尝试使用time.time或time.clock(天真地)来比较函数的速度。他们会给出误导性的结果。

PS.不要将打印语句放在您希望计时的函数中;否则测量的时间将取决于终端的速度。

其他回答

以下是我在阅读了许多好答案以及其他几篇文章之后的发现。

首先,如果你在timeit和time.time之间进行辩论,timeit有两个优点:

timeit选择操作系统和Python版本上可用的最佳计时器。然而,timeit禁用垃圾收集,这不是您可能想要或不想要的。

现在的问题是,时间并不是那么简单,因为它需要设置,当你有大量的导入时,事情会变得很糟糕。理想情况下,您只需要一个装饰器或使用块和度量时间。不幸的是,没有内置功能可用于此,因此您有两个选项:

选项1:使用时间预算库

timebudget是一个多功能且非常简单的库,您可以在pip安装后仅在一行代码中使用它。

@timebudget  # Record how long this function takes
def my_method():
    # my code

选项2:使用我的小模块

我创建了一个名为timing.py的计时实用程序模块。只需将这个文件放到项目中并开始使用它。唯一的外部依赖是runstats,它同样很小。

现在,只需在函数前面放置一个装饰器,就可以对任何函数计时:

import timing

@timing.MeasureTime
def MyBigFunc():
    #do something time consuming
    for i in range(10000):
        print(i)

timing.print_all_timings()

如果您想对代码的一部分计时,只需将其放入块中:

import timing

#somewhere in my code

with timing.MeasureBlockTime("MyBlock"):
    #do something time consuming
    for i in range(10000):
        print(i)

# rest of my code

timing.print_all_timings()

优势:

有几个半备份版本,所以我想指出几个亮点:

出于前面描述的原因,请使用timeit中的计时器,而不是time.time。如果需要,可以在计时期间禁用GC。Decorator接受带有命名或未命名参数的函数。能够在块计时中禁用打印(与timing.MeasureBlockTime()一起使用为t,然后为t.passed)。能够为块计时启用gc。

使用探查器模块。它提供了非常详细的概况。

import profile
profile.run('main()')

它输出类似于:

          5 function calls in 0.047 seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    0.000    0.000 :0(exec)
        1    0.047    0.047    0.047    0.047 :0(setprofile)
        1    0.000    0.000    0.000    0.000 <string>:1(<module>)
        0    0.000             0.000          profile:0(profiler)
        1    0.000    0.000    0.047    0.047 profile:0(main())
        1    0.000    0.000    0.000    0.000 two_sum.py:2(twoSum)

我发现它很有启发性。

这是另一种方法:

>> from pytictoc import TicToc
>> t = TicToc() # create TicToc instance
>> t.tic() # Start timer
>> # do something
>> t.toc() # Print elapsed time
Elapsed time is 2.612231 seconds.

与传统方式相比:

>> from time import time
>> t1 = time()
>> # do something
>> t2 = time()
>> elapsed = t2 - t1
>> print('Elapsed time is %f seconds.' % elapsed)
Elapsed time is 2.612231 seconds.

安装:

pip install pytictoc

有关详细信息,请参阅PyPi页面。

如何测量两次操作之间的时间。比较两次操作的时间。

import time

b = (123*321)*123
t1 = time.time()

c = ((9999^123)*321)^123
t2 = time.time()

print(t2-t1)

7.987022399902344e-05

这里有一个很好的文档记录和完全类型提示的装饰器,我将其用作通用工具:

from functools import wraps
from time import perf_counter
from typing import Any, Callable, Optional, TypeVar, cast

F = TypeVar("F", bound=Callable[..., Any])


def timer(prefix: Optional[str] = None, precision: int = 6) -> Callable[[F], F]:
    """Use as a decorator to time the execution of any function.

    Args:
        prefix: String to print before the time taken.
            Default is the name of the function.
        precision: How many decimals to include in the seconds value.

    Examples:
        >>> @timer()
        ... def foo(x):
        ...     return x
        >>> foo(123)
        foo: 0.000...s
        123
        >>> @timer("Time taken: ", 2)
        ... def foo(x):
        ...     return x
        >>> foo(123)
        Time taken: 0.00s
        123

    """
    def decorator(func: F) -> F:
        @wraps(func)
        def wrapper(*args: Any, **kwargs: Any) -> Any:
            nonlocal prefix
            prefix = prefix if prefix is not None else f"{func.__name__}: "
            start = perf_counter()
            result = func(*args, **kwargs)
            end = perf_counter()
            print(f"{prefix}{end - start:.{precision}f}s")
            return result
        return cast(F, wrapper)
    return decorator

示例用法:

from timer import timer


@timer(precision=9)
def takes_long(x: int) -> bool:
    return x in (i for i in range(x + 1))


result = takes_long(10**8)
print(result)

输出:耗时:4.942629056秒真的

可以通过以下方式检查doctest:

$ python3 -m doctest --verbose -o=ELLIPSIS timer.py

类型提示:

$ mypy timer.py