Question

I'm learning python and I was just playing around with the timeit module when I realize a weird behavior I couldn't explain.

#test.py

import timeit

def dictComp(I):
    return {x: x for x in I}

t = timeit.timeit(number=5,
                  stmt='dictComp(I)',
                  setup='from test import dictComp\nI=range(1000000)')
print(t)

So I'm trying to time the creation of dictionaries with a dict comprehension, by calling a user-defined function. But when I do that with the code above, timeit seems to execute twice. The interpreter output two different times.

But when I change the setup string to be from __main__ instead of from test, timeit runs only once (as expected).

So is there a difference between the two statements? Is it wrong to write from [module] import [obj] when the module is the main module? or does it have something to do with how the setup parameter of timeit works?

Please enlighten me. Cheers.

Était-ce utile?

La solution

The __main__ module is not the test module, even though they come from the same file. When execution hits

from test import dictComp

test.py executes again, this time as the test module instead of the main module. (When execution hits that line a second time, test is already being imported, so the file doesn't run a third time.)

It's good practice to put stuff like your timeit call in an if __name__ == '__main__' guard:

if __name__ == '__main__':
    t = timeit.timeit(...)

    print(t)

If you don't, then definitely import from __main__ as opposed to from test.

Autres conseils

Because you are including the test.py file twice once as __main__ and once as test check it out!

#test.py

import timeit

def dictComp(I):
    return {x: x for x in I}

print __name__
t = timeit.timeit(number=5,
                  stmt='dictComp(I)',
                  setup='from test import dictComp\nI=range(1000000)')
print(t)
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top