Reputation: 5508
I always read the code to calculate the time like this way:
%timeit function()
What does %
mean here?
I think, %
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.
Upvotes: 168
Views: 305666
Reputation: 11
You have to put %%timeit on the top of the cell. otherwise it will generate an Error
Upvotes: 1
Reputation: 76887
%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.
Upvotes: 172
Reputation: 191
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.
Upvotes: 18
Reputation: 29690
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)
:
Upvotes: 58
Reputation: 55448
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
Upvotes: 8
Reputation: 33
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.
Upvotes: 3