[Tutor] time vs. timeit

Chris “Kwpolska” Warrick kwpolska at gmail.com
Tue Aug 26 10:34:15 CEST 2014


On Tue, Aug 26, 2014 at 6:26 AM, diliup gabadamudalige
<diliupg at gmail.com> wrote:
> Hi all,
>
> 1. why do some say that the time module is more accurate than the timeit
> module?
> s = time.time()
> or
> s = timeit.timeit()
>
> 2. Why is it that both modules never return the same answer on each run?

The two functions have completely different uses, and do completely
different things.

>>> help(time.time)
Help on built-in function time in module time:

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

    Return the current time in seconds since the Epoch.
    Fractions of a second may be present if the system clock provides them.


In other words, return this: http://en.wikipedia.org/wiki/Unix_time


>>> help(timeit)
NAME
    timeit - Tool for measuring execution time of small code snippets.

[…]
     |  timeit(self, number=1000000)
     |      Time 'number' executions of the main statement.
     |
     |      To be precise, this executes the setup statement once, and
     |      then returns the time it takes to execute the main statement
     |      a number of times, as a float measured in seconds.  The
     |      argument is the number of times through the loop, defaulting
     |      to one million.  The main statement, the setup statement and
     |      the timer function to be used are passed to the constructor.

-- 
Chris “Kwpolska” Warrick <http://chriswarrick.com/>
PGP: 5EAAEA16
stop html mail | always bottom-post | only UTF-8 makes sense


More information about the Tutor mailing list