I want to measure the execution time of the Python method sayHello() using the method getExecutionTime(). They are both in a module and the getExecutionTime()-method should be callable from outside.
The code looks like this:
def getExecutionTime():
t = timeit.Timer("sayHello", "from __main__ import sayHello")
return t.timeit(2)
def sayHello():
print("Hello")
I keep getting an ImportError saying: “Cannot import name sayHello in File …”
I added the scope argument ("from __main__ import sayHello") to the Timer constructor to make the sayHello() method available within the timeit scope.
Note: I don’t want to call getExecutionTime() in the main part of this method, I want to that method from somewhere else.
Can anyone help?
Thanks.
I have a file that is literally this:
When run, it prints
Hellotwice and then the execution time. (Also note that it’ssayHello()rather thansayHelloin theTimerset up).Are you running this from within an IDE? From the command line & IDLE it works for me, under both Python 2.7 & 3.2.