python中的Timeit模块无法正常运行

时间:2014-05-14 22:35:32

标签: 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模块中存在不正确的空格/标签。发生了什么???

2 个答案:

答案 0 :(得分:1)

您可以像这样定义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行前面有一个缩进,这使得它们无效(因为它们在没有缩进的情况下执行)。所以你应该以不同的方式设置setup变量:

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 *'
    ))

答案 1 :(得分:0)

以下测试有效:

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()来消除它。

在运行有问题的函数之前打印变量总是一个好主意,但是你很困惑,错误绝对是神秘的。