Ask Your Question

computation time

asked 2019-07-02 08:21:02 +0200

santoshi gravatar image

I using timtit command to compute time the result is 625 loops, best of 3: 38.1 ns per loop what is the meaning of 625 loops, best of 3.

edit retag flag offensive close merge delete


?timeit would enlighten you...

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2019-07-02 11:44:12 +0200 )edit

No. But timeit? might.

vdelecroix gravatar imagevdelecroix ( 2019-07-02 14:45:40 +0200 )edit

1 Answer

Sort by » oldest newest most voted

answered 2019-07-02 14:29:41 +0200

Iguananaut gravatar image

updated 2019-07-02 14:32:53 +0200

You can check the documentation for the %timeit magic by running %timeit? and it will normally be displayed in a pager that you can scroll with the arrow keys (if in the console) or in the help window (in a notebook). To print the full docs to put here for posterity though, I did:

In [4]: import IPython.core.magics.execution

In [5]: print(IPython.core.magics.execution.ExecutionMagics.timeit.__doc__)
Time execution of a Python statement or expression

        Usage, in line mode:
          %timeit [-n<N> -r<R> [-t|-c] -q -p<P> -o] statement
        or in cell mode:
          %%timeit [-n<N> -r<R> [-t|-c] -q -p<P> -o] setup_code

        Time execution of a Python statement or expression using the timeit
        module.  This function can be used both as a line and cell magic:

        - In line mode you can time a single-line statement (though multiple
          ones can be chained with using semicolons).

        - In cell mode, the statement in the first line is used as setup code
          (executed but not timed) and the body of the cell is timed.  The cell
          body has access to any variables created in the setup code.

        -n<N>: execute the given statement <N> times in a loop. If this value
        is not given, a fitting value is chosen.

        -r<R>: repeat the loop iteration <R> times and take the best result.
        Default: 3

        -t: use time.time to measure the time, which is the default on Unix.
        This function measures wall time.

        -c: use time.clock to measure the time, which is the default on
        Windows and measures wall time. On Unix, resource.getrusage is used
        instead and returns the CPU user time.

        -p<P>: use a precision of <P> digits to display the timing result.
        Default: 3

        -q: Quiet, do not print result.

        -o: return a TimeitResult that can be stored in a variable to inspect
            the result in more details.


          In [1]: %timeit pass
          8.26 ns ± 0.12 ns per loop (mean ± std. dev. of 7 runs, 100000000 loops each)

          In [2]: u = None

          In [3]: %timeit u is None
          29.9 ns ± 0.643 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)

          In [4]: %timeit -r 4 u == None

          In [5]: import time

          In [6]: %timeit -n1 time.sleep(2)

        The times reported by %timeit will be slightly higher than those
        reported by the script when variables are accessed. This is
        due to the fact that %timeit executes the statement in the namespace
        of the shell, compared with, which uses a single setup
        statement to import function or create variables. Generally, the bias
        does not matter as long as results from are not mixed with
        those from %timeit.

Basically, in the default behavior, timeit will do multiple trials (typically three) where it loops over the function/code being timed some number of times, where the default number of loops is adjusted automatically based on the length of time taken the first few times through the loop. The result of each trial is an average time based on the number of loops. Multiple trials are performed just because depending on various external factors, each trial could give a different result, and it will give you the best result.

So in the case of "625 loops, best of 3: 38.1 ns per loop what is the meaning of 625 loops, best of 3", three trials were performed, in each trial the code was looped over 625 times, and "per loop" here means that the code took on average 38.1 ns for one pass through the loop, which is admittedly not entirely clear.

So in other words, it's saying out of 3 trials, it seems the average time for the code being timed is at best 38.1 ns. Though as you can see above there are many options for controlling the number of loops, the number of trials, etc.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2019-07-02 08:21:02 +0200

Seen: 349 times

Last updated: Jul 02 '19