Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'win32': 0.03; 'subject:Python': 0.06; 'function,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:skip:t 10': 0.09; 'sys.platform': 0.09; 'windows,': 0.09; 'python': 0.11; 'wrote': 0.14; '2.7': 0.14; '"win32":': 0.16; '0.03': 0.16; '0.04': 0.16; '2.7.3': 0.16; 'excerpt': 0.16; 'precision,': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'seconds.': 0.16; 'there?': 0.16; 'time.time()': 0.16; 'unix,': 0.16; 'wrote:': 0.18; 'feb': 0.22; '>>>': 0.22; 'platforms': 0.22; 'import': 0.22; 'header :User-Agent:1': 0.23; 'algorithms.': 0.24; 'processor': 0.24; 'script': 0.25; 'header:X-Complaints-To:1': 0.27; 'point': 0.28; 'function': 0.29; 'resolution': 0.29; '>>>>': 0.31; 'clock': 0.31; 'skip:q 20': 0.31; 'linux': 0.33; "i'd": 0.34; 'case,': 0.35; 'definition': 0.35; 'but': 0.35; 'subject:?': 0.36; 'seconds': 0.37; 'wrong': 0.37; 'expressed': 0.37; 'depends': 0.38; 'to:addr :python-list': 0.38; 'fact': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'number,': 0.60; 'most': 0.60; 'first': 0.61; 'here:': 0.62; 'become': 0.64; 'more': 0.64; '2014,': 0.84; 'florian': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: time.perf_counter in Python 2? Date: Mon, 20 Oct 2014 10:09:32 +0200 Organization: None References: Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit X-Gmane-NNTP-Posting-Host: p57bdb452.dip0.t-ipconnect.de User-Agent: KNode/4.13.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 69 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1413792597 news.xs4all.nl 2917 [2001:888:2000:d::a6]:36918 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:79310 Florian Lindner wrote: > Hello, > > I wrote a script that does some time measurements. It uses > time.perf_counter() from Python 3 which works very well. Now I need to > backport it to python 2. > > Docs say that time.clock() is way to go: > > 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. > > On Windows, this function returns wall-clock seconds elapsed since the > first call to this function, as a floating point number, based on the > Win32 function QueryPerformanceCounter(). The resolution is typically > better than one microsecond. > > But for me it always returns the almost same number, nothing time like: > > Python 2.7.3 (default, Feb 27 2014, 19:58:35) > [GCC 4.6.3] on linux2 > Type "help", "copyright", "credits" or "license" for more information. >>>> import time >>>> time.clock() > 0.03 >>>> time.clock() > 0.03 >>>> time.clock() > 0.04 >>>> time.clock() > 0.04 > > > What's wrong there? While clock is not the recommended timer on Linux the granularity is still higher over here: >>> time.clock() 0.06874 >>> time.clock() 0.069221 >>> time.clock() 0.069782 >>> time.clock() 0.070309 That or you can type faster ;) Here's an excerpt from the 2.7 timeit.py: if sys.platform == "win32": # On Windows, the best timer is time.clock() default_timer = time.clock else: # On most other platforms the best timer is time.time() default_timer = time.time In Python3.4 this has become default_timer = time.perf_counter So I'd go with timeit.default_timer().