Fast, simple and accurate Python timing. Written in Rust.
Find a file
2025-03-19 12:01:41 +00:00
.docs [24/11/23] Updating logo 2023-11-24 16:20:18 +00:00
.github/workflows [24/11/23] Fixing workflow file 2023-11-24 17:44:09 +00:00
src [2024-12-19] Suppress warning about implicit default 2024-12-19 09:43:22 +00:00
tests [2024-12-18] Move toc before tic test to its own test file 2024-12-18 16:15:49 +00:00
tictoc [2024-12-17] Allow for multiple timing operations 2024-12-17 10:11:31 +00:00
.gitignore [2024-12-16] Update .gitignore to ignore venv files 2024-12-16 07:52:03 +00:00
build.sh [2024-12-19] Add build script 2024-12-19 09:50:14 +00:00
Cargo.lock [2024-12-19] Bumping version number in Cargo.toml 2024-12-19 09:57:20 +00:00
Cargo.toml [2024-12-19] Bumping version number in Cargo.toml 2024-12-19 09:57:20 +00:00
CHANGELOG.md [2024-12-19] Add CHANGELOG.md 2024-12-19 08:50:09 +00:00
LICENSE.md [2024-12-18] Add LICENSE, update pyproject.toml for PyPi 2024-12-18 16:02:52 +00:00
pyproject.toml [2024-12-19] Add repo and changelog links to pyproject.toml 2024-12-19 09:50:03 +00:00
README.md [2025-03-19] Add total downloads badge to README 2025-03-19 12:01:41 +00:00
requirements.txt [2024-12-16] Bumping maturin and PyO3 versions for mod syntax 2024-12-16 10:34:41 +00:00
run.py [2024-12-18] Update README and run.py to reflect new syntax 2024-12-18 15:42:10 +00:00

Fast, simple and accurate Python timing. Written in Rust.

PyPI Downloads PyPI Downloads

Installation

Install with pip.

$ python -m pip install tictoc

Usage

Import.

from tictoc import tic,toc

Begin timing with tic(), and stop with toc().

tic()
# some code
toc()

A call to tic() can be followed with multiple toc() calls. Each will print the time elapsed since the most recent tic() call.

tic()
time.sleep(3)
toc()
# >>> The elapsed time was 3.000132333 seconds.
time.sleep(3)
toc()
# >>> The elapsed time was 6.000383124 seconds.

For more complex timing operations, you can assign the output of tic() and pass it as an input to toc().

Note

This syntax cannot be used interchangeably with the default syntax above. Any call to tic() resets the global timer.

firstTic = tic()
time.sleep(3)
secondTic = tic()
time.sleep(1)
toc(firstTic)
# >>> The elapsed time was 4.000317251 seconds.
time.sleep(3)
toc(secondTic)
# >>> The elapsed time was 4.000312568 seconds.

Any call to toc() will print the elapsed time in seconds. You can save the results with full precision by assigning the output of toc().

tic()
# some code
results = toc()

The available units are:

results.nanos   # u128
results.micros  # u128
results.millis  # u128
results.seconds # f64

Full example

import time
from tictoc import tic,toc

tic()         # start timing
time.sleep(3) # sleep for 3 seconds
toc()         # stop timing
# >>> The elapsed time was 3.000132333 seconds.

firstTic = tic()
time.sleep(3)
secondTic = tic()
time.sleep(1)
toc(firstTic)
# >>> The elapsed time was 4.000317251 seconds.
time.sleep(3)
toc(secondTic)
# >>> The elapsed time was 4.000312568 seconds.

tic()
results = toc()
print(results.nanos)
# >>> 2825