[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

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

hiral

3/12/2010 1:49:00 PM

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.la...
browse_thread/thread/8032897a30781df/c656a79d4c3268a6> it was fixed in
python 2.5.

Can anybody help.

Thank you.




3 Answers

Tim Roberts

3/15/2010 2:15:00 AM

0

hiral <hiralsmaillist@gmail.com> wrote:
>...
>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?

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, timr@probo.com
Providenza & Boekelheide, Inc.

hiral

3/15/2010 6:51:00 AM

0

On Mar 15, 7:14 am, Tim Roberts <t...@probo.com> wrote:
> hiral<hiralsmaill...@gmail.com> wrote:
> >...
> >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?
>
> 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, t...@probo.com
> Providenza & Boekelheide, Inc.- Hide quoted text -
>
> - Show quoted text -

Thanks for your explanation.

Gabriel Genellina

3/16/2010 5:50:00 AM

0

En Mon, 15 Mar 2010 03:51:28 -0300, hiral <hiralsmaillist@gmail.com>
escribió:
> On Mar 15, 7:14 am, Tim Roberts <t...@probo.com> wrote:
>> hiral<hiralsmaill...@gmail.com> wrote:

>> >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
>>
>> You can't really do an analysis like this with a task that only takes a
>> few
>> milliseconds. There are too many variables.
>
> Thanks for your explanation.

Have you tested with the highly recursive function in that 2007 thread you
linked to?
This should take time enough to make a problem like this clearly visible.

--
Gabriel Genellina