3

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???

2
  • 1
    How deeply indented are the contents of mySetup? They need to be completely unindented to work. I suspect they're indented to the same level as the rest of the function, like you'd do with a docstring. Commented May 14, 2014 at 22:40
  • That's exactly the issue. Thank you so much! Commented May 15, 2014 at 0:00

2 Answers 2

3

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 *'
    ))
Sign up to request clarification or add additional context in comments.

1 Comment

You can use Python's textwrap.dedent function for removing leading whitespace.
0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.