Time code using syntax similar to tic and toc in MATLAB
Project description
pytictoc contains a class TicToc which replicates the functionality of MATLAB’s tic and toc for easily timing sections of code. Under the hood, pytictoc uses the default_timer function from Python’s timeit module.
INSTALLATION
pytictoc can be installed and updated via conda or pip.
pip
pip install pytictoc pip install pytictoc --upgrade
conda
conda install pytictoc -c ecf conda update pytictoc -c ecf
USAGE
Basic usage:
>> from pytictoc import TicToc >> t = TicToc() #create instance of class >> t.tic() #Start timer >> t.toc() #Time elapsed since t.tic() Elapsed time is 2.612231 seconds.
A string passed to the toc method changes the printed message. This can be useful to differentiate timing of different sections in the same script.
>> t.toc('Section 1 took') Section 1 took 16.494467 seconds.
An optional keyword argument restarts the timer (equivalent to t.tic()) after reporting the time elapsed.
>> t.toc(restart=True) Elapsed time is 36.986837 seconds. >>t.toc() Elapsed time is 2.393425 seconds.
If you want to return the time elapsed to a variable rather than printing it, use the tocvalue method.
>>spam = t.tocvalue() >>spam 20.156261717544602
The TicToc class can be used within a context manager as an alternative way to time a section of code. The time taken to run the code inside the with statement will be reported on exit.
>>with TicToc(): >> spam = [x+1 for x in range(10000)] Elapsed time is 0.002343 seconds.
Determining and setting the timer
pytictoc uses timeit.default_timer to time code. On Python 3.3 and later, this is an alias for time.perf_counter. On earlier versions of Python it is an alias for the most precise timer for a given system.
To see which function is being used:
>>import pytictoc >>pytictoc.default_timer <function time.perf_counter>
You can change the timer by simple assignment.
>>import time >>pytictoc.default_timer = time.clock >>pytictoc.default_timer <function time.clock>
Project details
Release history Release notifications | RSS feed
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
File details
Details for the file pytictoc-1.5.1.tar.gz
.
File metadata
- Download URL: pytictoc-1.5.1.tar.gz
- Upload date:
- Size: 4.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0.post20200714 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.7.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3214dc7603095794178996c213ead519dd76107b353d5d0bc05a97d8af2774c3 |
|
MD5 | 9729831e5ef56c16a34a3a7d6e2b9b94 |
|
BLAKE2b-256 | db843dbdda3f949a49a8db801adc68a72e25f672644bb322628c27765c2cc076 |
File details
Details for the file pytictoc-1.5.1-py2.py3-none-any.whl
.
File metadata
- Download URL: pytictoc-1.5.1-py2.py3-none-any.whl
- Upload date:
- Size: 4.0 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0.post20200714 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.7.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8b48bb39e9625b879527c92d0fdbad0b28b16a918c2891ffee35f5eea4c21ec5 |
|
MD5 | 366891dc17d64402094756427b7a36ac |
|
BLAKE2b-256 | 17fac60631a899d6bb370f58d4cd95162ec3c204cd3987f2d65bd7c6a3270dd5 |