What is %timeit in Python?
Asked Answered
W

6

160

I always read the code to calculate the time like this way:

%timeit function()

What does "%" mean here?

I think, the "%" is always used to replace something in a string, like %s means replace a string, %d replace a data, but I have no idea about this case.

Watt answered 26/3, 2015 at 14:2 Comment(1)
That's a magic command from iPython. See: iPython MagicsBroida
W
166

%timeit is an IPython magic function, which can be used to time a particular piece of code (a single execution statement, or a single method).

From the documentation:

%timeit

Time execution of a Python statement or expression

Usage, in line mode:

%timeit [-n<N> -r<R> [-t|-c] -q -p<P> -o] statement

To use it, for example if we want to find out whether using xrange is any faster than using range, you can simply do:

In [1]: %timeit for _ in range(1000): True
10000 loops, best of 3: 37.8 µs per loop

In [2]: %timeit for _ in xrange(1000): True
10000 loops, best of 3: 29.6 µs per loop

And you will get the timings for them.

The major advantages of %timeit are:

  • You don't have to import timeit.timeit from the standard library, and run the code multiple times to figure out which is the better approach.

  • It will automatically calculate number of runs required for your code based on a total of 2 seconds execution window.

  • You can make use of current console variables implicitly, whereas timeit.timeit requires them to be provided explicitly.

Westbrooke answered 26/3, 2015 at 14:7 Comment(8)
is there a way to time a macro or a set of lines from the ipython shell?Ambrosia
@Ambrosia Yes, you can wrap those set of lines within a function, and then time the function like: %timeit function()Westbrooke
@mu無: Please clarify timer.timeit-- is that an actual module name?Pochard
@fountainhead: docs.python.org/3/library/timeit.html#timeit.timeitRichter
the output 10000 loops, best of 3: 29.6 µs per loop can be interpretted as: (1.) the expression is run 10000 times to get total time (2.) then total time is divided by 10000 to get "per loop" time and (3.) the total time computation is done 3 times and finally (4.) of the 3 total times, the minimum total time (aka "best of 3") is used as output. is my interpretation correct, yes/no?Mcphee
what is a mu-second?Fibriform
@Fibriform a microsecond, 1 millionth of a secondCyrillic
How do you run %timeit in VS Code? It says % is an invalid character.Pentheas
F
57

This is known as a line magic in IPython. They are unique in that their arguments only extend to the end of the current line, and magics themselves are really structured for command line development. timeit is used to time the execution of code.

If you wanted to see all of the magics you can use, you could simply type:

%lsmagic

to get a list of both line magics and cell magics.

Some further magic information from documentation here:

IPython has a system of commands we call magics that provide effectively a mini command language that is orthogonal to the syntax of Python and is extensible by the user with new commands. Magics are meant to be typed interactively, so they use command-line conventions, such as using whitespace for separating arguments, dashes for options and other conventions typical of a command-line environment.

Depending on whether you are in line or cell mode, there are two different ways to use %timeit. Your question illustrates the first way:

In [1]: %timeit range(100)

vs.

In [1]: %%timeit
      : x = range(100)
      :
Flood answered 26/3, 2015 at 14:7 Comment(0)
T
17

A very subtle point about %%timeit: Given it runs the "magics" on the cell, you'll get error...

UsageError: Line magic function %%timeit not found

...if there is any code/comment lines above %%timeit. In other words, ensure that %%timeit is the first command in your cell.

Theophany answered 14/8, 2020 at 12:12 Comment(1)
This is not applicable in the iPython shell, only in a Jupyter notebookMooned
L
7

IPython intercepts those. They're called built-in magic commands, and here's the list: Built-in magic commands.

You can also create your own custom magics, Defining custom magics.

Your timeit is here: %timeit

Logue answered 26/3, 2015 at 14:7 Comment(0)
R
2

Line magics are prefixed with the % character and work much like OS command-line calls: they get as an argument the rest of the line, where arguments are passed without parentheses or quotes. Cell magics are prefixed with a double %%, and they are functions that get as an argument not only the rest of the line, but also the lines below it in a separate argument.

Retreat answered 17/9, 2019 at 9:18 Comment(0)
Q
0

You have to put %%timeit on the top of the cell. otherwise it will generate an Error

Quarter answered 24/4 at 0:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.