Giter Club home page Giter Club logo

timer's Introduction

Description

A simple timer for measuring your code.

Usage

Simple timing

from Timer import Timer

timer = Timer()
timer.start()
# Code to be timed
timer.mark()
timer.print()
# Prints out something like: 'Time 1: 0.125s'

Multiple timing (continuous)

from Timer import Timer

timer = Timer()
timer.start()
# First code to be timed
timer.mark()
# Second code to be timed
timer.mark()
timer.print()
# Prints out something like: 
# Time 1: 0.125s
# Time 2: 0.226s

Multiple timing (non-continuous)

from Timer import Timer

timer = Timer()
timer.start()
# First code to be timed
timer.pause()
# Other code you don't want to time
timer.resume()
# Second code to be timed
timer.mark() # Or timer.pause()
timer.print()
# Prints out something like: 
# Time 1: 0.321s
# Time 2: 0.125s

Adding labels

from Timer import Timer

timer = Timer()
timer.start()
# First code to be timed
timer.pause('Processed data')
# Other code you don't want to time
timer.resume()
# Second code to be timed
timer.mark('Calculated complex operation') # Or timer.pause()
timer.print()
# Prints out something like: 
# Processed data: 0.321s
# Calculated complex operation: 0.125s

Get the times instead of printing

from Timer import Timer

timer = Timer()
timer.start()
# First code to be timed
timer.pause('Processed data')
# Other code you don't want to time
timer.resume()
# Second code to be timed
timer.mark('Calculated complex operation') # Or timer.pause()

# This will give a list of the timed values
times_measured = timer.times_measured()
# This will give a list of tuples with a pair (label, time)
times_measured_with_labels = timer.times_measured_with_labels()
# This will give only the last time measured
last_time = timer.latest_time_measured()

Chose where to print

from IPrinter import IPrinter
from Timer import Timer
from typing import AnyStr

# Create your printer class
class StringPrinter(IPrinter):
    def __init__(self):
        self._str = ''

    def print(self, string: AnyStr, end='\n'):
        self._str += string+end

    def content(self):
        return self._str
my_printer = StringPrinter()
timer = Timer()
timer.set_printer(my_printer)
timer.start()
# First code to be timed
timer.pause('Processed data')
# Other code you don't want to time
timer.resume()
# Second code to be timed
timer.mark('Calculated complex operation') # Or timer.pause()
# Will use the passed printer object, not the default one
timer.print()

timer's People

Contributors

rodrigowerberich avatar

Watchers

 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.