Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>According to the Python <a href="http://docs.python.org/library/timeit.html" rel="noreferrer">documentation</a> it has to do with the accuracy of the time function in different operating systems:</p> <blockquote> <p>The default timer function is platform dependent. On Windows, time.clock() has microsecond granularity but time.time()‘s granularity is 1/60th of a second; on Unix, time.clock() has 1/100th of a second granularity and time.time() is much more precise. On either platform, the default timer functions measure wall clock time, not the CPU time. This means that other processes running on the same computer may interfere with the timing ... On Unix, you can use time.clock() to measure CPU time.</p> </blockquote> <p>To pull directly from <code>timeit.py</code>'s code:</p> <pre><code>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 </code></pre> <p>In addition, it deals directly with setting up the runtime code for you. If you use time you have to do it yourself. This, of course <strong>saves you time</strong></p> <p>Timeit's setup:</p> <pre><code>def inner(_it, _timer): #Your setup code %(setup)s _t0 = _timer() for _i in _it: #The code you want to time %(stmt)s _t1 = _timer() return _t1 - _t0 </code></pre> <h2>Edit, Python 3 only:</h2> <p>Since Python 3.3 you can use <a href="http://docs.python.org/3/library/time.html#time.perf_counter" rel="noreferrer"><code>time.perf_counter()</code></a> (system-wide timing) or <a href="http://docs.python.org/3/library/time.html#time.process_time" rel="noreferrer"><code>time.process_time()</code></a> (process-wide timing), just the way you used to use <code>time.clock()</code>:</p> <pre><code>from time import process_time t = process_time() #do some stuff elapsed_time = process_time() - t </code></pre> <p>The new function <code>process_time</code> will not include time elapsed during sleep.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload