Skip to main content

A powerful multiline alternative to timeit

Project description

CircleCI Travis Appveyor Codecov Pypi Downloads ReadTheDocs CodeQuality

Timerit

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

Docs are being written at https://timerit.readthedocs.io/en/latest/

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 a standalone version of a utility distributed with 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 Distribution

timerit-0.3.0.tar.gz (10.6 kB view details)

Uploaded Source

Built Distribution

timerit-0.3.0-py2.py3-none-any.whl (13.8 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file timerit-0.3.0.tar.gz.

File metadata

  • Download URL: timerit-0.3.0.tar.gz
  • Upload date:
  • Size: 10.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.41.0 CPython/2.7.15

File hashes

Hashes for timerit-0.3.0.tar.gz
Algorithm Hash digest
SHA256 594178d0ff1b05909285b71dac1f1200d32cce45e9516b2824f2c3c76f154530
MD5 6a94aea6e3e84619c4ede88ac6a336f7
BLAKE2b-256 21c1ee92ecfd6842cacb71c0ededb7fa73d204c6f8a77d28b0a7a6a86fbf1f50

See more details on using hashes here.

File details

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

File metadata

  • Download URL: timerit-0.3.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 13.8 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.41.0 CPython/2.7.15

File hashes

Hashes for timerit-0.3.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 e869b929c8e7e569c2122eb7ad6bbc938704445f60557318df05b19343b65a6e
MD5 583efd202420d88637d8412de56c5474
BLAKE2b-256 0d3531fdb25ac646c41fe89faa5c5c78f223aed184c8aa6f1fc9faf2aa98ce0d

See more details on using hashes here.

Supported by

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