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

Dick Moores rdm at rcblue.com
Tue Oct 3 17:01:06 CEST 2006


At 05:24 AM 10/3/2006, Michael P. Reilly wrote:
>It is not really a "ghost".  On most systems, there is a split 
>between what happens while you are a "user" and what happens deep 
>inside the operation "system".  The function is showing you how much 
>time elapsed while in those two areas of the system.
>
>To show you what I mean, take a simple program that writes to the disk:
>
>def show_me_the_spam(breakfast):
>     file = open("spam.txt", "w")
>     for i in range(10:
>         print >>file, "spam, spam, %s and spam" % breakfast
>     file.close()
>show_me_the_spam("eggs")

(0.25, 0.15625, 0.0, 0.0, 0.0)  run included creating spam.txt
(0.171875, 0.09375, 0.0, 0.0, 0.0) after spam.txt already there

The (user time)/(system time) ratios are 1.6 and 1.83, respectively.

For my coinFlip.py I got
(0.3125, 4.015625, 0.0, 0.0, 0.0)

the (user time)/(system time) ratio was 0.079. Why the big 
difference? From your explanation I would have thought that your 
script would have a LOWER ratio than coinFlip.py because of the file 
operations.

Here's coinFlips.py and its result again:

==================================================
# 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
         else:
                 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)
  >>>


If I reduce flips from 1000000 to 1000, I get
 >>>
Evaluating 1coinFlip.py
  flips   heads  tails diff   %
1000 482 518 36 96.4 per cent
Time was 0.016 seconds
os.times() is (0.296875, 0.09375, 0.0, 0.0, 0.0)
 >>>

with a ratio of 3.17. So what's happening during the 1,000,000 loop 
that's put into "system" space?

>Most of the execution of the program would happen as the "user"; 
>things like calling functions, evaluating string parameters, 
>counting in the loop.
>
>But the parts that would need to talk to hardware, parts that might 
>be dangerous to have the program have direct control over or might 
>need to be shared with other programs at the same time (like a 
>printer or a keyboard or a disk drive) - these parts of the program 
>are hidden underneath function calls and statements like "open" and 
>"print" and put into a "system" space.
>
>How much time you spend in "system" and "user" space tells you how 
>CPU and resource intensive your program is.
>
>The 3rd and 4th values are the same, but cumulative for all that 
>programs "children".  A program can spawn subprocesses to help it 
>(see os.system and the like).
>
>All the above times are CPU times - time spent while the program has 
>been using fractions of CPU seconds.  The last value shows that 
>actual real duration of the program, similar to your (timeEnd - 
>timeStart), but a bit more accurate.  (Though it appears that value 
>isn't supported on your system?)

My system is
a Dell  Dimension 4600i  running Windows XP Professional Service Pack 
2 (build 2600).

Why wouldn't that value be supported? Should I call Dell? Bang on 
MS's door? (I live about a mile away.)  I feel a bit cheated. ;)

Thanks,  Michael, for your extended answer..

Dick






More information about the Tutor mailing list