pythonpython-3.xtimeit

timeit.timeit method error "expected an indented block"


As I am learning Python 3.5 and I wanted to start comparing time on different code. I have tried the timeit.timeit module on a couple other simple statements and got it to work.

I can't get it to work on the below code and get the error below:

Traceback (most recent call last):
File "C:\Users\ASUS\Desktop\pythoncode\class_gen.py", line 10, in <module>
    print(timeit.timeit(",".join(["#"+i+j for i in listTags for j in listTags if i != j])))
  File "C:\Python35\lib\timeit.py", line 213, in timeit
    return Timer(stmt, setup, timer, globals).timeit(number)
  File "C:\Python35\lib\timeit.py", line 133, in __init__
    code = compile(src, dummy_src_name, "exec")
  File "<timeit-src>", line 7
    _t1 = _timer()
      ^
IndentationError: expected an indented block"""

The code I am using is:

import itertools
import timeit

listTags = [ "TOT" , "WBA", "BUR", "SOU"]

print(timeit.timeit(",".join(["#" + "".join(t) for t in itertools.permutations(listTags, 2)]))

print(timeit.timeit(",".join(["#"+i+j for i in listTags for j in listTags if i != j])))

I have tried it with and without the number=number keyword to no avail.


Solution

  • You have to pass a python code as string in the timeit call.

    You were passing the evaluated value of your expression (#TOTWBA,#TOTBUR,#TOTSOU,#WBATOT,#WBABUR,#WBASOU,#BURTOT,#BURWBA,#BURSOU,#SOUTOT,#SOUWBA,#SOUBUR) to be executed.

    Moreover, you have to pass listTags literally, or using setup expression, as timeit runs in a different interpreter: it's not aware of your previously defined variables.

    I rewrote the first timeit call using single quotes to protect your statement and added a setup call to define the listTags variable.

    import itertools
    import timeit
    
    print(timeit.timeit('",".join(["#" + "".join(t) for t in itertools.permutations(listTags, 2)])',setup='listTags = [ "TOT" , "WBA", "BUR", "SOU"]'))
    

    I get a time as a result: 5.2231671576142285: it's working