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.
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.
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)
命令行将
repeat
选项设置为3:“最好的”是这三个中最好的。这与number参数不同,当您不设置
-n / number
参数时,它会自动为您确定。在另一方面,} documentation :
timeit.timeit()
函数不重复。它运行语句number,并给出总时间。从^{从^{} :
如果您想从中获得最佳结果,请使用^{} function :
但是,使用
timeit.repeat()
将不会为您自动设置编号。为此,您必须创建自己的Timer()
实例。在文档链接到implementation,因此您可以在
main()
函数中看到这是如何完成的;将其简化为使用默认选项时执行的代码:在Python3中,通过使用新的^{} method 和更好的缩放处理,上述功能得到了很大的改进。在
使用您的声明和设置进行演示:
^{pr2}$从manual:它运行主语句
number
次(默认为1000000
), 并返回执行所有number次所用的时间总和。在您可以将
number=10**8
添加到调用中,并查看结果如何更改:相关问题 更多 >
编程相关推荐