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

kwatch at gmail.com kwatch at gmail.com
Fri Feb 2 13:30:01 EST 2007


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.216666666667
  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




More information about the Python-list mailing list