Time the execution of Python code using syntax similar to MATLAB's tic and toc functions.
Project description
Time the execution of Python code using syntax similar to MATLAB's tic and toc functions.
Contents |
Installation
-
Using
pip
:pip install py-tictoc-timer
-
Using
pipenv
:pipenv install py-tictoc-timer
-
Using
poetry
:- In your
pyproject.toml
file, add:[tool.poetry.dependencies] py-tictoc-timer = "*"
Then in the terminal, run:poetry install
- Or run:
poetry add py-tictoc-timer
- In your
-
Using
conda
:conda install py-tictoc-timer
Usage
-
Basic usage:
>>> from py_tictoc_timer.tictoc import TicToc >>> from time import sleep >>> tt = TicToc() >>> tt.tic() >>> sleep(1.1) >>> tt.toc() Elapsed time: 1secs
-
Within context manager:
>>> from py_tictoc_timer.tictoc import TicToc >>> from time import sleep >>> with TicToc(): ... sleep(1.1) Elapsed time: 1secs
-
Within context manager using custom messages:
>>> from py_tictoc_timer.tictoc import TicToc >>> from time import sleep >>> with TicToc(begin_message="start", end_message="end"): ... sleep(1.1) start end: 1secs
-
Custom message:
>>> from py_tictoc_timer.tictoc import TicToc >>> from time import sleep >>> with TicToc("Total Time"): ... sleep(1.1) Total time: 1secs
-
With restart during
.tic()
:>>> from py_tictoc_timer.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 py_tictoc_timer.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 py_tictoc_timer.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 py_tictoc_timer.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
Contribution is always welcome!
- First, either fork or branch the main repo.
- Clone your forked/branched repo.
- Build your environment with any of the below options:
- With
pipenv
:if (-not (Test-Path .venv)) {mkdir .venv} python -m pipenv install --requirements requirements.txt --ignore-pipfile --skip-lock --no-site-packages python -m pipenv install --requirements requirements-dev.txt --dev --ignore-pipfile --skip-lock --no-site-packages python -m pipenv run pre-commit install
- With
poetry
on Windows:(Invoke-WebRequest -Uri https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py -UseBasicParsing).Content | python - python -m poetry run pre-commit install
- With
poetry
on Linux:curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/install-poetry.py | python - python -m poetry run pre-commit install
- With
- Start contributing.
- Ensure you add additional Unit Test's to the test library for each new feature/functionality.
- Ensure that all the tests are passing successfully.
- When you're happy with the changes, raise a Pull Request to merge with the main branch again.
Tests
-
Run Black:
pipenv run python -m black --safe py_tictoc_timer tests
-
Run PyTests:
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
Credit
This package was inspired by a few other packages:
Why you should use py-tictoc-timer
and not any of the others is because this package has:
- Better & more flexible restart to the timer
- Better custom messages during starting & ending the timer
- Enhanced usage within a context manager
Maintainers
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
Built Distribution
Hashes for py_tictoc_timer-1.1.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a8bb962129142c10fd14523ad5f4cde4ef2a8bc1bca755d4ab6fb41c2341194e |
|
MD5 | bcdef5f537156839efe6c2b88b088131 |
|
BLAKE2b-256 | 1d4582eb3b3ccf8d40127cc64e729087325f969e7b57aaea4fa588ce5a61f4e7 |