result of os.times() is different with 'time' command Options

H

hiral

Hi,

Python version: 2.6

Script:
def pt(start_time, end_time):
def ptime(time, time_str):
min, sec = divmod(time, 60)
hr, min = divmod(min, 60)
stmt = time_str + '\t'
if hr:
stmt += str(hr) + 'h'
stmt += str(min) + 'm' + str(sec) + 's'
print stmt

if start_time and end_time:
real_time = end_time[4] - start_time[4]
ptime(real_time, "real")
user_time = end_time[0] - start_time[0]
ptime(user_time, "user")
sys_time = end_time[1] - start_time[1]
ptime(sys_time, "sys")

import os, subprocess
cmd = ['ls']
print cmd
t1 = os.times()
subprocess.call(cmd)
t2 = os.times()
pt(t1, t2)
print ".end"


Output:
real 0.0m0.0100000002421s
user 0.0m0.0s
sys 0.0m0.0s


Command:
$ time ls

Output:
real 0m0.007s
user 0m0.000s
sys 0m0.000s


Is this the intended behaviour?

As per the link <http://groups.google.com/group/comp.lang.python/
browse_thread/thread/8032897a30781df/c656a79d4c3268a6> it was fixed in
python 2.5.

Can anybody help.

Thank you.
 
H

hiral

What is it that you are wondering about?  The formatting difference is due
to your code.  The difference between 10 milliseconds and 7 milliseconds
could be due to any number of things.  First, you have the overhead of
Python involved in your measurements.  Second, you have the variability of
memory caching and disk caching.  Your Python code causes /bin/ls to be
loaded into memory, and it's probably still in a file cache when you run
the second command.

You can't really do an analysis like this with a task that only takes a few
milliseconds.  There are too many variables.
--
Tim Roberts, (e-mail address removed)
Providenza & Boekelheide, Inc.- Hide quoted text -

- Show quoted text -

Thanks for your explanation.
 

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,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top