简体   繁体   English

timeit.timeit的返回值是平均值还是最佳值?

[英]Is the returned value of timeit.timeit the average or the best?

the doc tells us clearly that timeit Command-Line Interface outputs the best. 文档清楚地告诉我们,timeit命令行界面输出最佳。

python -m timeit '"-".join(map(str, range(100)))'
10000 loops, best of 3: 25.2 usec per loop

how about the Python Interface? Python接口怎么样?

>>> import timeit
>>> timeit.timeit('char in text', setup='text = "sample string"; char = "g"')
0.41440500499993504

is it still the best? 还是最好的吗?

The command line sets the repeat option to 3: 命令行将repeat选项设置为3:

 -r N, --repeat=N 

how many times to repeat the timer (default 3) 重复多少次计时器(默认3)

and 'the best' is the best out of those 3. That's different from the number parameter, which is determined automatically for you when you don't set the -n / --number parameter. “最好的”是这3个中的最好的。这与number参数不同,后者是在不设置-n / --number参数时自动为您确定的。

The timeit.timeit() function, on the other hand, doesn't repeat . 另一方面, timeit.timeit()函数不会重复 It runs the statement number of times, and gives you the total time . 它运行的时间语句数量 ,并为您的总时间 From the timeit.timeit() documentation : timeit.timeit()文档中

Create a Timer instance with the given statement, setup code and timer function and run its timeit() method with number executions. 使用给定的语句, 设置代码和计时器函数创建一个Timer实例,并通过数字执行运行其timeit()方法。

and from timeit.Timer.timeit() : 和从timeit.Timer.timeit()

Time number executions of the main statement. 主语句的时间编号执行。 This executes the setup statement once, and then returns the time it takes to execute the main statement a number of times , measured in seconds as a float. 这将执行一次setup语句,然后返回执行主语句所需的时间 (以秒为单位,以秒为单位)。

If you want to get a best out of result, use the timeit.repeat() function : 如果要获得最佳效果,请使用timeit.repeat()函数

  timeit.repeat(stmt='pass', setup='pass', timer=<default timer>, repeat=3, number=1000000) 

Create a Timer instance with the given statement, setup code and timer function and run its repeat() method with the given repeat count and number executions. 使用给定的语句, 设置代码和计时器函数创建一个Timer实例,并使用给定的重复计数和数字执行运行其repeat() method

Using timeit.repeat() will not auto-range the number for you however. 但是,使用timeit.repeat()不会自动为您调整数字范围。 For that you'd have to create your own Timer() instance. 为此,您必须创建自己的Timer()实例。

The documentation links to the implementation , so you can see how this is done in the main() function; 文档链接到该实现 ,因此您可以在main()函数中了解如何完成此操作; simplifying this down to the code that is executed when you use the default options: 将其简化为使用默认选项时执行的代码:

t = Timer(stmt, setup, timer)
repeat = 3
for i in range(1, 10):
    number = 10**i
    x = t.timeit(number)
    if x >= 0.2:
        break
r = t.repeat(repeat, number)
best = min(r)
print "%d loops," % number,
usec = best * 1e6 / number
if usec < 1000:
    print "best of %d: %.*g usec per loop" % (repeat, 3, usec)
else:
    msec = usec / 1000
    if msec < 1000:
        print "best of %d: %.*g msec per loop" % (repeat, 3, msec)
    else:
        sec = msec / 1000
        print "best of %d: %.*g sec per loop" % (repeat, 3, sec)

In Python 3, the above has been much improved with the new Timer.autorange() method and a better handling of scales. 在Python 3中,通过使用新的Timer.autorange()方法和更好地处理比例,对上述内容进行了很大的改进。

Demo using your statement and setup: 使用您的陈述和设置进行演示:

>>> import timeit
>>> t = timeit.Timer('char in text', setup='text = "sample string"; char = "g"')
>>> repeat = 3
>>> for i in range(1, 10):
...     number = 10**i
...     x = t.timeit(number)
...     if x >= 0.2:
...         break
...
>>> r = t.repeat(repeat, number)
>>> best = min(r)
>>> print "%d loops," % number,
10000000 loops,
>>> usec = best * 1e6 / number
>>> if usec < 1000:
...     print "best of %d: %.*g usec per loop" % (repeat, 3, usec)
... else:
...     msec = usec / 1000
...     if msec < 1000:
...         print "best of %d: %.*g msec per loop" % (repeat, 3, msec)
...     else:
...         sec = msec / 1000
...         print "best of %d: %.*g sec per loop" % (repeat, 3, sec)
...
best of 3: 0.0305 usec per loop

From the manual : It runs the main statement number times (default is 1000000 ), and returns the sum of time it took to execute all number times. 手册 :它运行的主要语句number倍(默认为1000000 ),并返回花费的时间执行所有次数的总和。

you can add number=10**8 to your call and see how the result changes: 您可以在通话中添加number=10**8并查看结果如何变化:

>>> import timeit
>>> timeit.timeit('char in text', setup='text = "sample string"; char = "g"')
0.03136014938354492
>>> timeit.timeit('char in text', setup='text = "sample string"; char = "g"', number=10**7)
0.22713899612426758
>>> timeit.timeit('char in text', setup='text = "sample string"; char = "g"', number=10**8)
2.130625009536743
>>> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM