python - 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 @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 模块中的空格/制表符不正确。这是怎么回事???

最佳答案

您可以像这样定义您的 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 *'
    ))

关于python - python 中的 Timeit 模块无法正常运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23666307/

相关文章:

python - 时间多个python脚本

python - 为什么这个 O(n^2) 解决方案比 O(n) 解决方案工作得更快?

python - timeit 模块 - 获得最快和最慢的循环

Python获取一周的开始和结束日期

python - 我怎样才能运行与 tkinter 一起不断循环的东西?

python -m timeit : SyntaxError: from __future__ imports must occur at the beginning of the file

python - 尝试在 Python 中为我的 sqrt 函数计时

python - 不要按住鼠标左键

python - 在 Windows 上使用 pip 从 GitHub 安装时出错

python - 将 pandas 中的数据框从迭代列表转换为适当的列和行