Skip to main content

Time the execution of Python code using syntax similar to MATLAB's tic and toc functions.

Project description

py-tictoc-timer

codecov Unit Testing Publish Package

Time the execution of Python code using syntax similar to MATLAB's tic and toc functions.

Installation

Using pip:

pip install py-tictoc-timer

Using pipenv:

pipenv install py-tictoc-timer

Using poetry:

  1. In your pyproject.toml file, add:
    [tool.poetry.dependencies]
    py-tictoc-timer = "*"
    
  2. Then in the terminal, run:
    poetry install
    

Using conda

conda install py-tictoc-timer

Usage

Basic usage:

>>> from tictoc import TicToc
>>> from time import sleep
>>> tt = TicToc()
>>> tt.tic()
>>> sleep(1.1)
>>> tt.toc()
Elapsed time: 1secs

Within context manager:

>>> from tictoc import TicToc
>>> from time import sleep
>>> with TicToc():
...     sleep(1.1)
Elapsed time: 1secs

Within context manager using custom messages:

>>> from tictoc import TicToc
>>> from time import sleep
>>> with TicToc(begin_message="start", end_message="end"):
...     sleep(1.1)
start
end: 1secs

Custom message:

>>> from tictoc import TicToc
>>> from time import sleep
>>> with TicToc("Total Time"):
...     sleep(1.1)
Total time: 1secs

With restart during .tic():

>>> from tictoc import TicToc
>>> from time import sleep
>>> tt = TicToc()
>>> tt.tic(restart=True)
>>> sleep(1.1)
>>> toc()
Elapsed time: 1secs
>>> toc()
Elapsed time: 1secs

With restart during .toc():

>>> from tictoc import TicToc
>>> from time import sleep
>>> tt = TicToc()
>>> tt.tic()
>>> sleep(1.1)
>>> tt.toc(restart=True)
Elapsed time: 1secs
>>> tt.toc()
Elapsed time: 1secs

With restart using .rtoc():

>>> from tictoc import TicToc
>>> from time import sleep
>>> tt = TicToc()
>>> tt.tic()
>>> sleep(1.1)
>>> tt.rtoc()
Elapsed time: 1secs
>>> tt.toc()
Elapsed time: 1secs

With time returned:

>>> from tictoc import TicToc
>>> from time import sleep
>>> tt = TicToc()
>>> tt.tic()
>>> sleep(1.1)
>>> value = tt.toc_value()
>>> print(round(value, 1))
1.1

Contribution

All contributions are welcome!

Build & Test

Run Black

python -m pipenv run python -m black --safe py_tictoc_timer tests

Run PyTests:

python -m pipenv run python -m pytest --verbose --cov=py_tictoc_timer --cov-report=term --cov-report=html:cov-report/html --cov-report=xml:cov-report/xml/cov-report.xml

Run MyPy Tests:

pipenv run mypy py_tictoc_timer --ignore-missing-imports --pretty --install-types --non-interactive

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

py-tictoc-timer-0.1.13.tar.gz (5.4 kB view hashes)

Uploaded Source

Built Distribution

py_tictoc_timer-0.1.13-py3-none-any.whl (5.3 kB view hashes)

Uploaded Python 3

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