What is %timeit in Python?

Question:

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.

Asked By: xirururu

||

Answers:

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

Answered By: bakkal

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)
      :
Answered By: miradulo

%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.

Answered By: Anshul Goyal

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.

Answered By: Arash Salehi

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.

Answered By: successfulmike