Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #79309 > unrolled thread

Re: time.perf_counter in Python 2?

Started byChris Angelico <rosuav@gmail.com>
First post2014-10-20 18:58 +1100
Last post2014-10-20 18:58 +1100
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: time.perf_counter in Python 2? Chris Angelico <rosuav@gmail.com> - 2014-10-20 18:58 +1100

#79309 — Re: time.perf_counter in Python 2?

FromChris Angelico <rosuav@gmail.com>
Date2014-10-20 18:58 +1100
SubjectRe: time.perf_counter in Python 2?
Message-ID<mailman.14994.1413791917.18130.python-list@python.org>
On Mon, Oct 20, 2014 at 6:47 PM, Florian Lindner <mailinglists@xgm.de> wrote:
> time.clock()
> On Unix, return the current processor time as a floating point number
> expressed in seconds. The precision, and in fact the very definition of the
> meaning of “processor time”, depends on that of the C function of the same
> name, but in any case, this is the function to use for benchmarking Python
> or timing algorithms.
>
> But for me it always returns the almost same number, nothing time like:

You're doing practically no work in there. You're using almost no
processor time - the interpreter's waiting for the user, not actually
busy. If you create a busyloop, you'll see time.clock() go up roughly
linearly:

rosuav@sikorsky:~$ python
Python 2.7.3 (default, Mar 13 2014, 11:03:55)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import time
>>> def killtime():
...     start=time.time()
...     end=time.clock()+1
...     while time.clock()<end: pass
...     return time.time()-start
...
>>> killtime()
1.0048420429229736
>>> killtime()
1.004641056060791
>>> killtime()
1.0081689357757568
>>> killtime()
1.0019969940185547
>>> killtime()
1.0044770240783691

This function will do its best to waste one second of CPU time. As you
see, it's taking pretty much exactly one second of wall time to do so
(as measured by time.time()). At the end of a few of these runs,
time.clock() will return a value that's fairly close to 1.0 times the
number of executions of killtime(), as the other work Python's doing
is insignificant by comparison.

ChrisA

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web