简体   繁体   English

Python的timeit模块返回多次重复的时间单位是什么?

[英]What is time unit returned by Python's timeit module for multiple repetitions?

I read that the time unit returned by the timeit module is seconds. 我读到timeit模块返回的时间单位是秒。

However, if I have multiple repetitions, eg 但是,如果我有多次重复,例如

min(timeit.Timer('my_function(t)', 
                      'from __main__ import my_function, t')
                              .repeat(repeat=50, number=1000)))

would I have to divide the result by 1000 to get the actual seconds per loop, or does it already account for that? 我是否必须将结果除以1000才能获得每个循环的实际秒数,或者它已经说明了这一点?

Yes, it is the total time which is returned. 是的,这是返回的总时间。 You have to divide by 1000 if you really want to have the time per iteration of the loop. 如果您确实想要循环的每次迭代都需要除以1000。 A simple test could have showed you that easily: 一个简单的测试可以轻松地向您显示:

>>> timeit.timeit("import time; time.sleep(1)", number=1)
1.0000581741333008
>>> timeit.timeit("import time; time.sleep(1)", number=5)
5.000285863876343
>>> timeit.repeat("import time; time.sleep(1)", number=5, repeat=3)
[5.000285863876343, 5.003287076950073, 5.000286102294922]

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

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