Python time measure question (timeit)

V

vedrandekovic

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?
2) Are this results in micro seconds?


Regards,
John
 
S

Steve Holden

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
 
V

vedrandekovic

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..


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

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()


b) myscript.py code:

import sys
print sys.argv[1]
var1=int(sys.argv[2])
var2=int(sys.argv[3])
var3=var1+var2


Regards,
John
 
S

Steve Holden

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top