Python time measure question (timeit)
steve at holdenweb.com
Sun Feb 1 13:05:11 EST 2009
vedrandekovic at yahoo.com wrote:
> On 1 velj, 17:42, Steve Holden <st... at holdenweb.com> wrote:
>> vedrandeko... at yahoo.com wrote:
>>> When I run following code with os.popen (for this time measure I'm
>>> using python module timeit):
>>> for i in range(50):
>>> print i
>>> I get this result: 0.00246958761519
>>> But when I run same code from IDLE i get this result:
>>> now, I have two questions:
>>> 1) Which of this results is correct?
>> Both are. What makes you think that printing in IDLE (to a GUI) should
>> take the same amount of time as it does "running with os.popen (whatever
>> that means). The two environments are likely to be completely different,
>> but you haven't show the exact code you ran so it's hard to be more exact.
>>> 2) Are this results in micro seconds?
>> No, seconds, as it says in the documentation:
>> """This executes the setup statement once, and then returns the time it
>> takes to execute the main statement a number of times, measured in
>> seconds as a float""".
>> Even a modern computer doesn't do much in .0024 microseconds.
>> Steve Holden +1 571 484 6266 +1 800 494 3119
>> Holden Web LLC http://www.holdenweb.com/
> Hello again,
> This is the code that I ran:
> a) Main code:
> This code is in .exe:
> t = Timer("import os;os.popen('python myscript.py arg1 2 3')")
> print t.timeit()
Here in order to run the code os.popen() has to create a new subprocess
and initialize the Python interpreter inside it before finally running
myscript.py. I'd say that 2.5 milliseconds is quite acceptable for that.
> b) myscript.py code:
> import sys
> print sys.argv
In the IDLE case I presume you were running the code directly? Obviously
that eliminates the interpreter startup overhead, and so will be much
quicker. 65 microseconds, if I read the times correctly.
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/
More information about the Python-list