Git Product home page Git Product logo

pytictoc's Introduction

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.

The default message can also be passed in the constructor, for easier usage with context managers:

>> with TicToc(default_msg='Section 1 took'):
>>     ...
Section 1 took 16.494467 seconds.

The string passed to toc will override the string in the constructor if both are present.

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

You can also pass in an alternative stream to print to:

>> t = TicToc(stream=mystream)

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, or by passing a different timer function into an object's constructor.

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

>> import time
>> pytictoc.TicToc(timer=time.clock)

pytictoc's People

Contributors

mbarnathan avatar ericcfields avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.