Skip to main content

A powerful multiline alternative to timeit

Project description

Travis Codecov Appveyor Pypi

Timerit

A powerful multiline alternative to Python’s builtin timeit module.

Description

Easily do robust timings on existing blocks of code by simply indenting them. There is no need to refactor into a string representation or convert to a single line.

This is the standalone version of a utility currently in ubelt.

Installation

From pypi:

pip install timerit

From github:

pip install git+https://github.com/Erotemic/timerit.git

Examples

The quick and dirty way just requires one indent.

>>> import math
>>> from timerit import Timerit
>>> for _ in Timerit(num=200, verbose=2):
>>>     math.factorial(10000)
Timing for 200 loops
Timed for: 200 loops, best of 3
    time per loop: best=2.469 ms, mean=2.49 ± 0.037 ms

Use the loop variable as a context manager for more accurate timings or to incorporate an setup phase that is not timed. You can also access properties of the Timerit class to programmatically use results.

>>> import math
>>> from timerit import Timerit
>>> t1 = Timerit(num=200, verbose=2)
>>> for timer in t1:
>>>     setup_vars = 10000
>>>     with timer:
>>>         math.factorial(setup_vars)
>>> print('t1.total_time = %r' % (t1.total_time,))
Timing for 200 loops
Timed for: 200 loops, best of 3
    time per loop: best=2.064 ms, mean=2.115 ± 0.05 ms
t1.total_time = 0.4427177629695507

There is also a simple one-liner that is comparable to IPython magic:

Compare the timeit version:

>>> %timeit math.factorial(100)
564 ns ± 5.46 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

With the Timerit version:

>>> Timerit(100000).call(math.factorial, 100).print()
Timed for: 1 loops, best of 1
    time per loop: best=4.828 µs, mean=4.828 ± 0.0 µs

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

timerit-0.0.1-py2.py3-none-any.whl (8.4 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file timerit-0.0.1-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for timerit-0.0.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 7215da4d51e148ca57b3f33a5991cfb2daaf74ed841e658306737699fc882373
MD5 a3815f310b602ee3190bad5e52dbd168
BLAKE2b-256 04022b3e461b0634994dce16df71c9b0019ef5e19d82b881997c5a9c9a289c56

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page