2

So I can use time.clock() to measure the running time of a function call:

>>> def f() :
  a = time.clock()
  range(pow(10,8))
  b = time.clock()
  print a,b

>>> f()
0.143698 8.345905

But now if just call time.clock() repeatedly from the interactive shell:

>>> time.clock()
0.075492
>>> time.clock()
0.075931
>>> time.clock()
0.076354
>>> time.clock()
0.076754
>>> time.clock()
0.077132
...

What is the meaning of these numbers?

And now if I do this:

>>> def g() :
      a = time.clock()
      time.sleep(10)
      b = time.clock()
      print a,b

>>> g() 
8.361528 8.361625

Ok I guess that sleep is not counted in processing time, so the two numbers are very close. But what does 8.361528 correspond to?

I did read the documentation, but I still don't understand:

Help on built-in function clock in module time:

clock(...) clock() -> floating point number

Return the CPU time or real time since the start of the process or since
the first call to clock().  This has as much precision as the system
records.
usual me
  • 8,338
  • 10
  • 52
  • 95
  • 3
    See e.g. http://stackoverflow.com/a/25317322/3001761 – jonrsharpe Aug 27 '14 at 12:50
  • @jonrsharpe: I must be stupid, but I still don't understand the meaning of the numbers 0.077 and 8.36. – usual me Aug 27 '14 at 13:16
  • @usualme: don't interpret the absolute values, interpret the *relative differences*. That's the only think you'd care about. – Martijn Pieters Aug 27 '14 at 13:27
  • `time.clock()` behavior depends on platform. [Use `timeit.default_timer()` instead](http://stackoverflow.com/questions/85451/python-time-clock-vs-time-time-accuracy#comment18341094_85536). – jfs Oct 28 '14 at 07:52

3 Answers3

1

What system?

"This method returns the current processor time as a floating point number expressed in seconds on Unix and in Windows it returns wall-clock seconds elapsed since the first call to this function, as a floating point number."

http://www.tutorialspoint.com/python/time_clock.htm

Gunnar
  • 11
  • 1
  • This is more of a comment than an answer. – chepner Aug 27 '14 at 13:28
  • Since the authoring user's reputation does not [suffice](http://meta.stackexchange.com/a/25791) to [comment](http://stackoverflow.com/help/privileges/comment) at this point, posting an answer seems the only option. – J. Katzwinkel Aug 27 '14 at 13:36
1

time.clock() appears return the time the Python process has spent computing things. In the interactive console at least, the time it spends waiting for your input is somehow not included in that time.

Here's a snippet you can use to see more expected behavior of time.clock():

while True: print clock()
interestinglythere
  • 1,230
  • 13
  • 16
0

time.clock() docs:

On Unix, return the current processor time as a floating point number expressed in seconds...
On Windows, this function returns wall-clock seconds elapsed since the first call to this function...

i.e., the behavior (e.g., what "processor time" means, whether the sleep time is included, what is the precision) depends on platform but units are seconds on all platforms.

So I can use time.clock() to measure the running time of a function call:

Use timeit.default_timer() instead. It is assigned to time.time() or time.clock() depending on OS. On Python 3.3+ default_timer is time.perf_counter() on all platforms.

It returns the value in seconds (only relative values are meaningful). It includes "sleep" time. It provides the highest available resolution to measure a short duration. The behavior is consistent across platforms.

jfs
  • 399,953
  • 195
  • 994
  • 1,670
  • Can it also be used for timing code such as internet requests (`requests.get('www.google.com')`) or database queries? – usual me Oct 29 '14 at 13:36
  • @usualme: yes. `timeit.default_timer()` measures a wall clock time. – jfs Oct 29 '14 at 14:51