iPython %timeit magic command in normal Python files
Project description
Timeit-magic
iPython %timeit
magic command in normal Python files.
from timeit_magic import timeit
def foo(n):
return sum(list(range(n)))
timeit("foo(10_000)", globals=globals())
timeit("foo(10_000_000)", number=10, repeat=1, globals=globals())
timeit("foo(10_000_000)", max_time=20, globals=globals())
>> 99.9 µs ± 752 ns per loop (mean ± std. dev. of 7 runs, 20000 loops each)
229 ms ± 0 ns per loop (mean ± std. dev. of 1 runs, 10 loops each)
230 ms ± 2.33 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
Timeit-magic allows you to time code in a .py
file with the same output and behavior as the %timeit
command in iPython and Jupyter Notebook.
It also provides another function, time_it
, which allows you to time a function by decorating it:
from timeit_magic import time_it
@time_it
def foo(n):
return sum(list(range(n)))
# Both time_it and timeit returns the total runtime in seconds
total_time = foo(10_000)
print(total_time)
>> 101 µs ± 1.53 µs per loop (mean ± std. dev. of 7 runs, 20000 loops each)
14.110744676014292
Both functions accepts the optional arguments repeat
and number
, which does the same as -r
and -n
in %timeit
.
For more usage information, see the function docstrings and the timeit
documentation.
Installation
Timeit-magic can be installed from PyPI by using $ pip install timeit-magic
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
No source distribution files available for this release.See tutorial on generating distribution archives.
Built Distribution
Close
Hashes for timeit_magic-0.2.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6391c226a690a36f1b3e0d2207c8d04ec0af0c64a33f9e685bc7031ef5992fb6 |
|
MD5 | 637b2c42d46c1f9493fd316e4dcb755d |
|
BLAKE2b-256 | a4b2bce96d0e71ea72f17c1b552c940e43ea712584b005e1b393c457a54985da |