我正在尝试将 timeit 模块用于 python,它看起来好像 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 @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
我知道错误很可能发生在键盘和椅子之间,但我收到的错误似乎表明 timeit 模块中的空格/制表符不正确。到底是怎么回事???