The reason Y0 is undefined is that you have determined what is in the string, but at the time of parsing at the start of execution, the string has not yet been evaluated to bring the variable to life. So put Y0 = 0 somewhere at the top of your script to define it in advance.
All external functions and variables must be assigned to Timer using the setup argument. Therefore, you need "import myfunctions; X0 = 1" as the setting.
This will work:
from timeit import Timer import myfunctions X0 = 1 Y0 = 0
See how I used "X0 = %i" % (X0,) to pass the actual value of the external variable X0.
Another thing you might want to know is that if there are any functions in your main file that you want to use in timeit , you can make timeit recognize them by passing from __main__ import * as the second argument .
If you want timeit to timeit able to modify your variables, you should not pass strings to them. More conveniently, you can transfer calls to him. You must pass a callable that will change your desired variable. Then you do not need to setup . Take a look:
from timeit import Timer import myfunctions def measure_me(): global Y0
As you can see, I put print Y0 after print t.timeit() , since you cannot change its value before execution!
source share