473,796 Members | 2,569 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

[Q] result of os.times() is different with 'time' command

Hi,

I have a question about os.times().
os.times() returns a tuple containing user time and system time,
but it is not matched to the result of 'time' command.
For example, os.times() reports that user time is 39.85 sec,
but 'time' command reports that user time is 28.55sec.
(machine: Python2.5, MacOS X 10.4 Tiger, MacBook 1.83GHz intel core
duo)

file: ostimetest.py
--------------------
import os

## benchmark function
def f(x):
if x <= 1:
return 1
else:
return f(x-1) + f(x-2)

## do benchmark
n = 35
t1 = os.times() # start time
v = f(n) # end time
print "n=%d, v=%d" % (n, v)
t2 = os.times()

## print result
utime = t2[0] - t1[0] # user time
stime = t2[1] - t1[1] # system time
print "utime=%s, stime=%s" % (utime, stime)
--------------------

Result:
=============== =====
$ python -V
Python 2.5
$ time python ostimetest.py
n=35, v=14930352
utime=39.85, stime=0.2166666 66667
real 0m28.554suser 0m23.938ssys 0m0.177s
=============== =====

This shows that os.times() reports that user time is 39.85sec,
but time command shows that user time is 23.938sec.
Why os.times() reports wrong result? Do I have any mistake?

--
kwatch

Feb 2 '07 #1
4 3524

I dont see anything wrong !
Did you try to measure time with your watch ?
Did you try a simple python test.py without the time command ?
Maybe python is 'disturbed' by the intel core

Here my result on a linux dual AMD, python 2.4.3

# time python test.py
n=35, v=14930352
utime=22.54, stime=0.02

real 0m22.755s
user 0m22.564s
sys 0m0.022s

can you try this ?

# strace python test.py 2>&1 | grep time
times({tms_utim e=1, tms_stime=1, tms_cutime=0, tms_cstime=0}) =
430217777
times({tms_utim e=2238, tms_stime=2, tms_cutime=0, tms_cstime=0}) =
430220049
write(1, "n=35, v=14930352\nuti me=22.37, st"..., 41n=35, v=14930352
utime=22.37, stime=0.01

now you can compare what your system replied and what python
returned !
On 2 fév, 19:30, kwa...@gmail.co m wrote:
Hi,

I have a question about os.times().
os.times() returns a tuple containing user time and system time,
but it is not matched to the result of 'time' command.
For example, os.times() reports that user time is 39.85 sec,
but 'time' command reports that user time is 28.55sec.
(machine: Python2.5, MacOS X 10.4 Tiger, MacBook 1.83GHz intel core
duo)

file: ostimetest.py
--------------------
import os

## benchmark function
def f(x):
if x <= 1:
return 1
else:
return f(x-1) + f(x-2)

## do benchmark
n = 35
t1 = os.times() # start time
v = f(n) # end time
print "n=%d, v=%d" % (n, v)
t2 = os.times()

## print result
utime = t2[0] - t1[0] # user time
stime = t2[1] - t1[1] # system time
print "utime=%s, stime=%s" % (utime, stime)
--------------------

Result:
=============== =====
$ python -V
Python 2.5
$ time python ostimetest.py
n=35, v=14930352
utime=39.85, stime=0.2166666 66667
real 0m28.554suser 0m23.938ssys 0m0.177s
=============== =====

This shows that os.times() reports that user time is 39.85sec,
but time command shows that user time is 23.938sec.
Why os.times() reports wrong result? Do I have any mistake?

--
kwatch

Feb 2 '07 #2
[various posting problems corrected and response interspersed in
previous post for purposes of coherent response]

In article <11************ **********@v33g 2000cwv.googleg roups.com>,
"aspineux" <as******@gmail .comwrites:
On 2 Feb, 19:30, kwa...@gmail.co m wrote:
Hi,

I have a question about os.times().
os.times() returns a tuple containing user time and system time,
but it is not matched to the result of 'time' command.
For example, os.times() reports that user time is 39.85 sec,
but 'time' command reports that user time is 28.55sec.
(machine: Python2.5, MacOS X 10.4 Tiger, MacBook 1.83GHz intel core
duo)

[ source elided ]

I dont see anything wrong !
Did you try to measure time with your watch ?
Did you try a simple python test.py without the time command ?
Maybe python is 'disturbed' by the intel core

can you try this ?

# strace python test.py 2>&1 | grep time
times({tms_utim e=1, tms_stime=1, tms_cutime=0, tms_cstime=0}) =
430217777
times({tms_utim e=2238, tms_stime=2, tms_cutime=0, tms_cstime=0}) = 430220049
write(1, "n=35, v=14930352\nuti me=22.37, st"..., 41n=35, v=14930 52
utime=22.37, stime=0.01
Note that this likely won't work. First, strace is not native to
OS X; ktrace is the analogous native command. Second, OS X almost
certainly implements the times system call in terms of getrusage.
Result:
=============== =====
$ python -V
Python 2.5
$ time python ostimetest.py
n=35, v=14930352
utime=39.85, stime=0.2166666 66667
real 0m28.554suser 0m23.938ssys 0m0.177s
=============== =====

This shows that os.times() reports that user time is 39.85sec,
but time command shows that user time is 23.938sec.
Why os.times() reports wrong result? Do I have any mistake?

--
kwatch
Yes, I can reproduce this on my FreeBSD system. No, I do not believe
that you have made a mistake. Yes, I believe that you have uncovered
a bug in the Python os/posix modules.

Here's my analysis (although I should note that I've not looked
at the source of Python previously). I'm looking at Python 2.4.3,
but this looks like a long existing bug:

The following code exists in the source code module
Modules/posixmodule.c @ posix_times:
struct tms t;
clock_t c;
[ ... ]
c = times(&t);
[ ... ]
return Py_BuildValue(" ddddd",
(double)t.tms_u time / HZ,
(double)t.tms_s time / HZ,
(double)t.tms_c utime / HZ,
(double)t.tms_c stime / HZ,
(double)c / HZ);
This is incorrect. It should not be dividing by HZ, but by the
result of the dynamic value 'sysconf (_SC_CLK_TCK)'. Even if
it were to use a compile time value, the proper value would be
CLK_TCK, not HZ.

So here's what's happening. Neither my FreeBSD nor the OP's Mac
defines HZ as a compile time variable, but the same source module
also contains the following code:
#ifndef HZ
#define HZ 60 /* Universal constant :-) */
#endif /* HZ */
So, the Python posix module is using 60 instead of the proper
value, which happens to be 128 on my FreeBSD, and 100 on the OP's
OS X(*). (BTW, this sort of historic code is exactly why POSIX
no longer defines HZ.)

In support of this, I note that the following ratios exist:
user time from os.times / user time from time command
39.85 / 23.938 =1.665
CLK_TCK / HZ
100 / 60 =1.667
which are in reasonably close agreement!

- dmw
[*] I've actually only looked at OS X for the PPC platform, not
for the User's Intel platform, but I'm fairly certain that the
CLK_TCK value is the same on both.

--
.. Douglas Wells . Connection Technologies .
.. Internet: -sp9804- -at - contek.com- .
Feb 3 '07 #3

Your analysis looks great, maybe the good arguments to report a bug ?
On 3 fév, 04:27, se*@signature.i nvalid (Douglas Wells) wrote:
[various posting problems corrected and response interspersed in
previous post for purposes of coherent response]

In article <11************ **********@v33g 2000cwv.googleg roups.com>,

"aspineux" <as******@gmail .comwrites:
On 2 Feb, 19:30, kwa...@gmail.co m wrote:
Hi,
I have a question about os.times().
os.times() returns a tuple containing user time and system time,
but it is not matched to the result of 'time' command.
For example, os.times() reports that user time is 39.85 sec,
but 'time' command reports that user time is 28.55sec.
(machine: Python2.5, MacOS X 10.4 Tiger, MacBook 1.83GHz intel core
duo)
[ source elided ]
I dont see anything wrong !
Did you try to measure time with your watch ?
Did you try a simple python test.py without the time command ?
Maybe python is 'disturbed' by the intel core
can you try this ?
# strace python test.py 2>&1 | grep time
times({tms_utim e=1, tms_stime=1, tms_cutime=0, tms_cstime=0}) =
430217777
times({tms_utim e=2238, tms_stime=2, tms_cutime=0, tms_cstime=0}) = 430220049
write(1, "n=35, v=14930352\nuti me=22.37, st"..., 41n=35, v=14930 52
utime=22.37, stime=0.01

Note that this likely won't work. First, strace is not native to
OS X; ktrace is the analogous native command. Second, OS X almost
certainly implements the times system call in terms of getrusage.
Result:
=============== =====
$ python -V
Python 2.5
$ time python ostimetest.py
n=35, v=14930352
utime=39.85, stime=0.2166666 66667
real 0m28.554suser 0m23.938ssys 0m0.177s
=============== =====
This shows that os.times() reports that user time is 39.85sec,
but time command shows that user time is 23.938sec.
Why os.times() reports wrong result? Do I have any mistake?
--
kwatch

Yes, I can reproduce this on my FreeBSD system. No, I do not believe
that you have made a mistake. Yes, I believe that you have uncovered
a bug in the Python os/posix modules.

Here's my analysis (although I should note that I've not looked
at the source of Python previously). I'm looking at Python 2.4.3,
but this looks like a long existing bug:

The following code exists in the source code module
Modules/posixmodule.c @ posix_times:
struct tms t;
clock_t c;
[ ... ]
c = times(&t);
[ ... ]
return Py_BuildValue(" ddddd",
(double)t.tms_u time / HZ,
(double)t.tms_s time / HZ,
(double)t.tms_c utime / HZ,
(double)t.tms_c stime / HZ,
(double)c / HZ);
This is incorrect. It should not be dividing by HZ, but by the
result of the dynamic value 'sysconf (_SC_CLK_TCK)'. Even if
it were to use a compile time value, the proper value would be
CLK_TCK, not HZ.

So here's what's happening. Neither my FreeBSD nor the OP's Mac
defines HZ as a compile time variable, but the same source module
also contains the following code:
#ifndef HZ
#define HZ 60 /* Universal constant :-) */
#endif /* HZ */
So, the Python posix module is using 60 instead of the proper
value, which happens to be 128 on my FreeBSD, and 100 on the OP's
OS X(*). (BTW, this sort of historic code is exactly why POSIX
no longer defines HZ.)

In support of this, I note that the following ratios exist:
user time from os.times / user time from time command
39.85 / 23.938 =1.665
CLK_TCK / HZ
100 / 60 =1.667
which are in reasonably close agreement!

- dmw
[*] I've actually only looked at OS X for the PPC platform, not
for the User's Intel platform, but I'm fairly certain that the
CLK_TCK value is the same on both.

--
. Douglas Wells . Connection Technologies .
. Internet: -sp9804- -at - contek.com- .

Feb 4 '07 #4
Thank you, aspineux and Douglas.

Douglas's analysis is especially great.
I changed HZ to CLK_TCK in Python-2.5/Modules/posixmodule.c and
got the proper result!

=============== =====
$ time /usr/local/python2.5/bin/python ostimetest.rb
n=35, v=14930352
utime=12.42, stime=0.04

real 0m13.621s
user 0m12.438s
sys 0m0.063s
=============== =====

Great job, Douglas.

--
regards,
kwatch
s...@signature. invalid (Douglas Wells) wrote:
[various posting problems corrected and response interspersed in
previous post for purposes of coherent response]

In article <1170446796.156 141.201...@v33g 2000cwv.googleg roups.com>,

"aspineux" <aspin...@gmail .comwrites:
On 2 Feb, 19:30, kwa...@gmail.co m wrote:
Hi,
I have a question about os.times().
os.times() returns a tuple containing user time and system time,
but it is not matched to the result of 'time' command.
For example, os.times() reports that user time is 39.85 sec,
but 'time' command reports that user time is 28.55sec.
(machine: Python2.5, MacOS X 10.4 Tiger, MacBook 1.83GHz intel core
duo)
[ source elided ]
I dont see anything wrong !
Did you try to measure time with your watch ?
Did you try a simple python test.py without the time command ?
Maybe python is 'disturbed' by the intel core
can you try this ?
# strace python test.py 2>&1 | grep time
times({tms_utim e=1, tms_stime=1, tms_cutime=0, tms_cstime=0}) =
430217777
times({tms_utim e=2238, tms_stime=2, tms_cutime=0, tms_cstime=0}) = 430220049
write(1, "n=35, v=14930352\nuti me=22.37, st"..., 41n=35, v=14930 52
utime=22.37, stime=0.01

Note that this likely won't work. First, strace is not native to
OS X; ktrace is the analogous native command. Second, OS X almost
certainly implements the times system call in terms of getrusage.
Result:
=============== =====
$ python -V
Python 2.5
$ time python ostimetest.py
n=35, v=14930352
utime=39.85, stime=0.2166666 66667
real 0m28.554suser 0m23.938ssys 0m0.177s
=============== =====
This shows that os.times() reports that user time is 39.85sec,
but time command shows that user time is 23.938sec.
Why os.times() reports wrong result? Do I have any mistake?
--
kwatch

Yes, I can reproduce this on my FreeBSD system. No, I do not believe
that you have made a mistake. Yes, I believe that you have uncovered
a bug in the Python os/posix modules.

Here's my analysis (although I should note that I've not looked
at the source of Python previously). I'm looking at Python 2.4.3,
but this looks like a long existing bug:

The following code exists in the source code module
Modules/posixmodule.c @ posix_times:
struct tms t;
clock_t c;
[ ... ]
c = times(&t);
[ ... ]
return Py_BuildValue(" ddddd",
(double)t.tms_u time / HZ,
(double)t.tms_s time / HZ,
(double)t.tms_c utime / HZ,
(double)t.tms_c stime / HZ,
(double)c / HZ);
This is incorrect. It should not be dividing by HZ, but by the
result of the dynamic value 'sysconf (_SC_CLK_TCK)'. Even if
it were to use a compile time value, the proper value would be
CLK_TCK, not HZ.

So here's what's happening. Neither my FreeBSD nor the OP's Mac
defines HZ as a compile time variable, but the same source module
also contains the following code:
#ifndef HZ
#define HZ 60 /* Universal constant :-) */
#endif /* HZ */
So, the Python posix module is using 60 instead of the proper
value, which happens to be 128 on my FreeBSD, and 100 on the OP's
OS X(*). (BTW, this sort of historic code is exactly why POSIX
no longer defines HZ.)

In support of this, I note that the following ratios exist:
user time from os.times / user time from time command
39.85 / 23.938 =1.665
CLK_TCK / HZ
100 / 60 =1.667
which are in reasonably close agreement!

- dmw
[*] I've actually only looked at OS X for the PPC platform, not
for the User's Intel platform, but I'm fairly certain that the
CLK_TCK value is the same on both.

--
. Douglas Wells . Connection Technologies .
. Internet: -sp9804- -at - contek.com- .

Feb 4 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
2524
by: yzhshi | last post by:
hello, the follow code get different result from Command line and browser that is to say ,i get the correct result from the command line,but i cant say the result from browser. IPC::RUN is used in this code; i get it from http://ppm.activestate.com/PPMPackages/zips/8xx-builds-only/Windows/IPC-Run-0.77.zip my environment is: Microsoft windows 2000 professional
10
12845
by: KENNY L. CHEN | last post by:
Dear experts, I have two tables in my Oracle 8i database: TEST (COL1,COl2,REC_NO) and TEST1 (COL1,COL2,REC_NO). Both tables are unique-indexed on (COL1,COL2,REC_NO). I think the following SQL commands will return the same result but one of my friends don't think so. He said "QUERY 1" will return 1 unsorted record (ROWNUM < 2 ) first then sort the result (ORDER BY COL1 ASC,
5
6801
by: adolf garlic | last post by:
Im trying to return xml from sql. The xml is made up of different fragments, some using FOR XML ... syntax. The result is a valid xml doc. There is a working stored proc that returns the xml In .net i'm having problems loading this up. I've now tried installing sqlxml managed classes and the following appears to work when stepping through, but the result just disappears.
2
1809
by: Steve | last post by:
A main form contains ten textboxes and ten subforms. All ten subforms have the same query (Query2) for their recordsource. Query2 is based on a query (Query1). Each subform is linked to a different textbox (LinkMaster) on the main form. Each textbox has a different value so each subform displays different records. Ques 1: When the form opens, how many times does Query1 run? Ques 2: When the form opens, how many times does Query2 run?
14
2101
by: Tom.PesterDELETETHISSS | last post by:
Hi, I think this question requires an in depth understanding of how a browser cache works. I hope I can reach an expert here. I may have found a quirk in the asp.net documentation or I don't understand what the SetAllowResponseInBrowserHistory does. While researching caching I tried the code sample at the following page : http://msdn2.microsoft.com/library/97wcd0a4(en-us,vs.80).aspx
7
3538
by: Bernard Lebel | last post by:
Hello, I'm stumbled at a serious problem, and quite frankly getting desparate. This is a rather long-winded one so I'll try to get straight to the point. I have this Python program, that performs MySQL queries to a database. These queries are performed at regular intervals. Basically it is looking for fields that match certain criterias. Such fields are not present at all time. When the program finds such field, it then takes
2
7515
by: archana | last post by:
Hi all, I am facing some wired problem while using above mention data type. What i am doing is i am writing DateTime.Now.Hour.ToString() + ":" + DateTime.Now.Minute.ToString() + ":" + DateTime.Now.Second.ToString() + ":"+DateTime.Now.Millisecond.ToString() to the file.
15
1904
by: wizofaus | last post by:
I have a chunk of code which is essentially IDbCommand cmd = db.CreateCommand(); cmd.CommandText = "SELECT X, Y, Count(*) FROM Foo WHERE Z = 1 GROUP BY X, Y"; using (IDataReader reader = cmd.ExecuteReader()) while (reader.Read()) { // grab values from query }
6
5932
by: RandomElle | last post by:
Hi there I'm hoping someone can help me out with the use of the Eval function. I am using Access2003 under WinXP Pro. I can successfully use the Eval function and get it to call any function with or without parms. I know that any function that is passed to Eval() must be declared Public. It can be a Sub or Function, as long as it's Public. I even have it where the "function" evaluated by Eval can be in a form (class) module or in a standard...
0
9680
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9528
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10455
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10173
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6788
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5441
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5573
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4116
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3731
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.