Fast, simple and accurate Python timing. Written in Rust.
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
Languages
Python
47.1%
Rust
36.6%
Shell
16.3%