[Tutor] what is "user time" in os.times()?

Dick Moores rdm at rcblue.com
Tue Oct 3 11:46:52 CEST 2006

In a recent(ongoing?) thread on python-list, "PATCH: Speed up direct 
string concatenation by 20+%!", Larry Hastings, the op, says
he computed his benchmark times using 
"sum(os.times()[:2])".  Curious, I looked up os.times and tried it 
out on a little script I wrote to demonstrate the Law of Large 
Numbers (to myself):

# coinFlip.py

import time, os
from random import choice

heads, tails = 0, 0
flips = 1000000

timeStart = time.time()
for x in xrange(flips):
	coin = choice(["heads", "tails"])
	if coin == "heads":
		heads += 1
		tails += 1
timeEnd = time.time()
osTimes = os.times
difference = abs(heads - tails)
print " flips   heads  tails diff   %"
print flips, heads, tails, difference, 100 - 
(100*difference*1.0/flips), "per cent"
print "Time was %.4g seconds" % (timeEnd - timeStart)
print "os.times() is", osTimes()

Evaluating 1coinFlip.py
  flips   heads  tails diff   %
1000000 500902 499098 1804 99.8196 per cent
Time was 4.156 seconds
os.times() is (0.3125, 4.015625, 0.0, 0.0, 0.0)

 From the os module doc for times():

"Return a 5-tuple of floating point numbers indicating accumulated 
(processor or other) times, in seconds. The items are: user time, 
system time, children's user time, children's system time, and 
elapsed real time since a fixed point in the past, in that order. See 
the Unix manual page times(2) or the corresponding Windows Platform 
API documentation. Availability: Macintosh, Unix, Windows."

Now, I won't ask (yet) about what the 3rd, 4th, and 5th elements of 
the 5-tuple are, but I'd like to know what the first one, user time, 
is. Who is this ghost in the machine?

The man page times(2) was no help, to me anyway. 

Dick Moores


More information about the Tutor mailing list