Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

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

5 views
Skip to first unread message

hiral

unread,
Mar 12, 2010, 8:49:04 AM3/12/10
to
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.


Tim Roberts

unread,
Mar 14, 2010, 10:14:56 PM3/14/10
to
hiral <hiralsm...@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, ti...@probo.com
Providenza & Boekelheide, Inc.

hiral

unread,
Mar 15, 2010, 2:51:28 AM3/15/10
to
On Mar 15, 7:14 am, Tim Roberts <t...@probo.com> wrote:
> Tim Roberts, t...@probo.com
> Providenza & Boekelheide, Inc.- Hide quoted text -
>
> - Show quoted text -

Thanks for your explanation.

Gabriel Genellina

unread,
Mar 16, 2010, 1:50:29 AM3/16/10
to pytho...@python.org
En Mon, 15 Mar 2010 03:51:28 -0300, hiral <hiralsm...@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

0 new messages