简体   繁体   English

无法了解时间输出

[英]Cannot understand timeit output

my problem is this: 我的问题是这样的:

>>> 999 in list(xrange(1000))
True # this takes a glimpse

Instead: 代替:

>>> import timeit
>>> timeit.timeit('999 in list(xrange(1000))')
26.88947892189026

Why does timeit gives such a high output? 为什么timeit会提供这么高的输出?

Thank you 谢谢

Because it does this 1 million times ! 因为这样做一百万次
https://docs.python.org/3.4/library/timeit.html#timeit.timeit https://docs.python.org/3.4/library/timeit.html#timeit.timeit


timeit.timeit takes a positional argument number which defaults to 1.000.000 so it indeed takes longer to do the statement 1 million times compared to only one. timeit.timeit需要一个位置参数number默认为1.000.000所以它确实需要更长的时间做了发言100万次相比,只有一个。

If you do this 1 time, it's way faster : 如果您执行1次,则速度会更快:

In [2]: timeit.timeit('999 in list(xrange(1000))')
Out[2]: 31.733104944229126

In [3]: timeit.timeit('999 in list(xrange(1000))', number=1)
Out[3]: 4.5061111450195312e-05
timeit.timeit(stmt='pass', setup='pass', timer=<default timer>, number=1000000)

Stupid question, the default number of times the statement is executed is one million... I should have read timeit doc before... 愚蠢的问题,该语句的默认执行次数为一百万。。。我应该已经读过timeit doc了。

>>> timeit.timeit('999 in list(xrange(1000))', number=1)
9.083747863769531e-05

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

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