python中的Timeit模块未正确运行
我试图使用python的timeit模块,它看起来好像timeit源代码中有错误(尽管这似乎不正确) 下面是正在运行的代码片段:python中的Timeit模块未正确运行,python,timeit,Python,Timeit,我试图使用python的timeit模块,它看起来好像timeit源代码中有错误(尽管这似乎不正确) 下面是正在运行的代码片段: def recordCuckoo(amtElements, loadFactor): ''' Determines the average lookup speed in seconds of a cuckoo hash table with @amtElements elements and a load factor of @loadFac
def recordCuckoo(amtElements, loadFactor):
'''
Determines the average lookup speed in seconds of a cuckoo hash table
with @amtElements elements and a load factor of @loadFactor
'''
mySetup = '''
import Statistics
import random
import hashingLibrary
from CuckooHashing import *
'''
controlStatement = "Statistics.timeCuckooControl(" + str(amtElements) + "," + str(loadFactor) + ")"
testStatement = "Statistics.timeCuckoo(" + str(amtElements) + "," + str(loadFactor) + ")"
controlTime = timeit.timeit(controlStatement, setup=mySetup, number=1)
testTime = timeit.timeit(testStatement, setup=mySetup, number=1)
lookupTime = (testTime - controlTime)/1000000
print ("The average lookup time for a cuckoo table with {0} elements and a load factor of {1} was:".format(amtElements, loadFactor))
print (lookupTime)
return lookupTime
if __name__ == "__main__":
recordCuckoo(100, 0.5)
运行时,我收到以下错误:
Traceback (most recent call last):
File "C:\Python34\CuckooHashing\Statistics.py", line 308, in <module>
recordCuckoo(100, 0.5)
File "C:\Python34\CuckooHashing\Statistics.py", line 267, in recordCuckoo
controlTime = timeit.timeit(controlStatement, setup=mySetup, number=1)
File "C:\Python34\lib\timeit.py", line 213, in timeit
return Timer(stmt, setup, timer).timeit(number)
File "C:\Python34\lib\timeit.py", line 122, in __init__
code = compile(src, dummy_src_name, "exec")
File "<timeit-src>", line 9
_t0 = _timer()
^
IndentationError: unindent does not match any outer indentation level
回溯(最近一次呼叫最后一次):
文件“C:\Python34\buckoohashing\Statistics.py”,第308行,在
布谷鸟(100,0.5)
RecordBuckoo中第267行的文件“C:\Python34\buckoohashing\Statistics.py”
controlTime=timeit.timeit(controlStatement,setup=mySetup,number=1)
文件“C:\Python34\lib\timeit.py”,第213行,在timeit中
返回计时器(stmt、设置、计时器)。timeit(数字)
文件“C:\Python34\lib\timeit.py”,第122行,在\uuu init中__
代码=编译(src,dummy\u src\u name,“exec”)
文件“”,第9行
_t0=_计时器()
^
缩进错误:未缩进与任何外部缩进级别不匹配
我知道这个错误很可能发生在键盘和椅子之间,但我收到的错误似乎表明timeit模块中的空格/制表符不正确。发生了什么事?以下测试有效:
timeit.timeit('a+3', setup='a=1', number=10000)
timeit.timeit('a+3 -float(2\n)', setup='a=1', number=10000)
timeit.timeit(' a+3 -float(2\n)', setup='a=1', number=10000)
但由于您的错误,此操作失败:
timeit.timeit('a+3', setup=' a=1', number=10000)
请注意设置中的空格。您可以通过传递mySetup.strip()
来消除它
在运行有问题的函数之前打印变量始终是一个好主意,但您有权感到困惑,错误是绝对神秘的。您将
mySetup
变量定义如下:
mySetup = '''
import Statistics
import random
import hashingLibrary
from CuckooHashing import *
'''
如果你只考虑这一点,那根本不是问题。但是,这些行实际上出现在函数声明中:
def recordCuckoo(amtElements, loadFactor):
mySetup = '''
import Statistics
import random
import hashingLibrary
from CuckooHashing import *
'''
因此,实际上,mySetup
的内容如下:
'''
import Statistics
import random
import hashingLibrary
from CuckooHashing import *
'''
如您所见,import
行前面有一个缩进,这使得它们无效(因为它们在执行时没有预期的缩进)。因此,应以不同的方式设置设置变量:
def recordCuckoo(amtElements, loadFactor):
mySetup = '''
import Statistics
import random
import hashingLibrary
from CuckooHashing import *
'''
或者像他这样的人:
def recordCuckoo(amtElements, loadFactor):
mySetup = '\n'.join((
'import Statistics',
'import random',
'import hashingLibrary',
'from CuckooHashing import *'
))
mySetup
的内容缩进有多深?他们需要完全不受影响地工作。我怀疑它们与函数的其余部分缩进到了相同的级别,就像您使用docstring一样。这正是问题所在。非常感谢你!就是这样。现在可以了。非常感谢你!可以使用Python函数删除前导空格。