Python time measure question (timeit)
Steve Holden
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:
>>> Hello,
>>> 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:
>>> 6.4533341528e-005
>>> 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.
>>
>> regards
>> Steve
>> --
>> 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[1]
> var1=int(sys.argv[2])
> var2=int(sys.argv[3])
> var3=var1+var2
>
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.
regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/
More information about the Python-list
mailing list