两者之间是否有任何显着差异:
from time import time
start = time()
# some process
print time() - start
和:
from timeit import timeit
def my_funct():
# some process
print timeit(my_funct, number=1)
例如,我将使用Project Euler 1(因为它真的很容易理解/解决)
def pE1test1(): # using time()
from time import time
start = time()
print sum([n for n in range(1, 1000) if n%3==0 or n%5==0])
print time() - start
def pE1test2(): # using timeit
print sum([n for n in range(1, 1000) if n%3==0 or n%5==0])
from timeit import timeit
pE1test1()
print timeit(pE1test2, number=1)
这输出:
>>>
233168
0.0090000629425
233168
0.00513921300363
timeit
和之间的主要区别是什么time
?