Domanda

I am trying to use the timeit module for python and it appears as if there's an error in the timeit source code (though this doesn't seem right).

Here's the snippet of the code being run:

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)

And I receive the following error when I run it:

 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

I understand that the error is most likely between the keyboard and chair, but the error I'm receiving appears to indicate that there's an improper space/tab in the timeit module. What is going on???

È stato utile?

Soluzione

You define your mySetup variable like this:

mySetup = '''
import Statistics
import random
import hashingLibrary
from CuckooHashing import *
'''

If you just consider that, it’s not a problem at all. However, those lines actually appear within a function declaration:

def recordCuckoo(amtElements, loadFactor):
    mySetup = '''
    import Statistics
    import random
    import hashingLibrary
    from CuckooHashing import *
    '''

So, actually, the content of mySetup is the following:

'''
    import Statistics
    import random
    import hashingLibrary
    from CuckooHashing import *
    '''

As you can see, there is an indentation in front of the import lines, which makes them invalid (because they are executed with no indentation expected). So you should set the setup variable in a different way:

def recordCuckoo(amtElements, loadFactor):
    mySetup = '''
import Statistics
import random
import hashingLibrary
from CuckooHashing import *
'''

or maybe something like his:

def recordCuckoo(amtElements, loadFactor):
    mySetup = '\n'.join((
        'import Statistics',
        'import random',
        'import hashingLibrary',
        'from CuckooHashing import *'
    ))

Altri suggerimenti

The following tests work:

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)

But this fails with your error:

timeit.timeit('a+3', setup=' a=1', number=10000)

Note the space in setup. You can get rid of it by passing mySetup.strip() instead.

It is always a good idea to print your variables before running a problematic function, but you are right to be confused, the error is absolutely cryptic.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top