Skip to main content

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 timer.perf_counter>

You can change the timer by simple assignment.

>>import time
>>pytictoc.default_timer = time.clock
>>pytictoc.default_timer
<function timer.clock>

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

pytictoc-1.4.0.tar.gz (3.7 kB view details)

Uploaded Source

Built Distribution

pytictoc-1.4.0-py2.py3-none-any.whl (4.1 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file pytictoc-1.4.0.tar.gz.

File metadata

  • Download URL: pytictoc-1.4.0.tar.gz
  • Upload date:
  • Size: 3.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for pytictoc-1.4.0.tar.gz
Algorithm Hash digest
SHA256 81d24ab8b92fd92dbd2517c4c2330af69a0a32e548a7166377b98707f7e41d92
MD5 8fa354a17b474ca9478ef7946ce5548a
BLAKE2b-256 2ef3ef89668e4e704ecc2fe2d53123ee29da61f719cac127ae37ef5c84dc65c8

See more details on using hashes here.

File details

Details for the file pytictoc-1.4.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for pytictoc-1.4.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 2bac0f24d024d9b126d5ea1e1fd9669820d27c013e845c97fb763d8ec78e9191
MD5 443cc35a7f8e7e26ccf0d32637a36222
BLAKE2b-256 eb410b7c0682975b53abb62884b97b0e72a22962354df9c50c4a9c82847b5281

See more details on using hashes here.

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