Re: time.perf_counter in Python 2?
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(). -- https://mail.python.org/mailman/listinfo/python-list
Re: time.perf_counter in Python 2?
On Mon, Oct 20, 2014 at 6:47 PM, Florian Lindner 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()>> 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 -- https://mail.python.org/mailman/listinfo/python-list
time.perf_counter in Python 2?
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? Thanks, Florian -- https://mail.python.org/mailman/listinfo/python-list