ticktock is a minimalist library to view Python time performance of Python code.

Overview

ticktock

Simple Python code metering library.


ticktock is a minimalist library to view Python time performance of Python code.

First, install ticktock:

pip install py-ticktock

Then, anywhere in your code you can use tick to start a clock, and tock to register the end of the snippet you want to time:

t = tick()
# do some work
t.tock()

Even if the code is called many times within a loop, measured times will only periodially (2 seconds by default):

import time
from ticktock import tick

for _ in range(1000):
    t = tick()
    # do some work
    time.sleep(1)
    t.tock()

You can create multiple independent ticks, which will appear as two separate clocks:

for _ in range(1000):
    t = tick()
    # do some work
    time.sleep(1)
    t.tock()

    t = tick()
    # do some other work
    time.sleep(0.5)
    t.tock()

Ticks can share a common starting point:

for k in range(1000):
    t = tick()
    # do some work
    time.sleep(1)
    if k % 2 == 1:
        time.sleep(1)
        t.tock()
    else:
        t.tock()

It is also possible to use ticktock as a context manager to track the timing of a chunk of code:

from ticktock import ticktock

with ticktock():
    time.sleep(1)

Or a decorator, to track the timing of each call to a function:

from ticktock import ticktock

@ticktock
def f():
    time.sleep(1)

f()
Comments
  • feature request: suspended mode

    feature request: suspended mode

    Hi,

    I could not find in documentation: is there a way of temporarily shutting off all ticktock calls? This is helpful for switching between debug and production modes.

    Thanks!

    opened by gilbh 8
  • Suppress line numbers in `tock()`

    Suppress line numbers in `tock()`

    Hello.

    Thanks for your effort on the library, works really nice.

    I would like to polish the output a bit - i.e. suppress the line numbers from the tock event, since In some cases they are redundant. For a :

    @ticktick("aaa")
    def a():
       pass
    

    I want: [aaaa] 1s200 ms and now I have [aaaa:3-5] 1s200 ms

    And for a:

    a_clock = tick(name="train batch")
    a_clock.tock()
    

    I want: [train batch] 100ms and now I get [train batch:11] 100ms

    Rationale: In the annotation case - this is obvious that a method with decorator will just get a single tock() for every tick - and so there is no need to differentiate the times, and hence it would be best if the output shown would just contain the name passed to @ticktock()

    In the manual case - I would say, that this should be user-controlled. I am not sure what the API would be best, but maybe something like tick(single_tock=True) or tock(only=True) that would made the tock() not record any name for the tock and therefore the ouptut might just contain the name passed to tick ?

    opened by kretes 5
  • feature request: easier control on renderer

    feature request: easier control on renderer

    Hi,

    I noticed there is no carriage return in the ticktock message, so I wrote the following, but I was wondering if there could be a less verbose way of doing this:

    from ticktock import ticktock
    from ticktock.timer import ClockCollection, set_collection
    from ticktock import renderers
    
    set_collection(
        collection=ClockCollection(renderer=renderers.StandardRenderer(format=renderers.StandardRenderer()._format + '\n')))
    
    
    @ticktock
    def test():
        for aa in range(100_000):
            pass
    
    
    test()
    print('done')
    

    Thanks!

    opened by gilbh 5
  • bug: ImportError: cannot import name 'ticktock' from 'ticktock'

    bug: ImportError: cannot import name 'ticktock' from 'ticktock'

    Hi,

    Maybe it's something I don't get, but I can't get pass the import statement:

    ImportError: cannot import name 'ticktock' from 'ticktock' (c:\users\gil\envs\utils\lib\site-packages\ticktock.py)
    > c:\dropbox\documents_and_writings\research\digital_humanities\python\stocks\trade_ampf.py(34)<module>()
         32 from shutil import copyfile
         33 from itertools import repeat
    ---> 34 from ticktock import ticktock
    

    The pip install went fine....

    Running on Windows ...

    Thanks!

    opened by gilbh 1
  • Fix name

    Fix name

    This fixes a bug encountered when using ticktock in a REPL with unnamed tick calls.

    In this context, clock.tick_filename does not exist, and name_field_fn fails because tick_name is used before being defined. This was broken recently.

    The first commit adds a test to expose the bug, while the second fixes it.

    opened by victorbenichoux 0
  • Renderer per clock + fix to decorator and context manager + refacto

    Renderer per clock + fix to decorator and context manager + refacto

    This PR is relatively large, and achieves the following:

    • Add the capability to have different formatting strings for different clocks, which adresses the requests in https://github.com/victorbenichoux/ticktock/issues/12 as well as https://github.com/victorbenichoux/ticktock/issues/4
    • Simplifies the format string, with a general name field that works in decorators/context managers as one would expect
    • Refactor by splitting ticktock.timer into ticktock.clocks with clocks, ticktock.collection with collection, ticktock.timers for decorator/contextmanager interfaces
    • Refactor by removing largely unused Clock and tick parameters (e.g. tick_time_ns)
    • Make clock identity consistent by never disambiguating clocks on their names
    • Better tests of context manager and function decorator functionality
    • numerous improvements to the documentation
    opened by victorbenichoux 0
  • More formatting options

    More formatting options

    This PR introduces more formatting options, by enabling the user to choose amongst more keys, for example the tick line, tock line, but also the raw timings.

    This added flexibility adds a bit more constraints: in particular, format strings should also contain the clock name part:

    set_format("⏱️ [{tick_name}-{tock_name}] {mean} ({std} std) min={min} max={max} count={count} last={last}")
    

    There are also a couple of minor refactorizations.

    opened by victorbenichoux 0
  • Set formatting

    Set formatting

    • Add a global set_format function to set the format of ticktock messages
    • Allow setting max_terms through set_format (https://github.com/victorbenichoux/ticktock/issues/4)
    • Create a no_update flag in StandardRenderer to avoid print ASCII control chars
    opened by victorbenichoux 0
  • Default two precision

    Default two precision

    This PR fixes https://github.com/victorbenichoux/ticktock/issues/6 by adding another level of precision to the times that are shown by the StandardRenderer.

    This can be controlled by setting max_terms on the StandardRenderer, which defaults to 2.

    opened by victorbenichoux 0
  • MultipleRenderers - delegates rendering to multiple renderers

    MultipleRenderers - delegates rendering to multiple renderers

    This is a small facade over renderers that we use for e.g. render at the same time to stdout and to external monitoring system. It would be nice to have it in the library

    opened by kretes 2
  • Allow to configure the behaviour of StandardRenderer

    Allow to configure the behaviour of StandardRenderer

    Currently we use StandardRenderer with no_update=True and it is used in a process that uses tqdm (keras training loop). The effect is that the first line from ticktock is appended at the end of the line of current tqdm progress. I can see in https://github.com/victorbenichoux/ticktock/blob/main/ticktock/std.py#L137 there is a new line appended at the end of ticktock output. We would like to add a new line at the beginning as well. WDYT of either including it be default or allowing to customize it by the client?

    opened by kretes 1
Releases(v0.1.0)
  • v0.1.0(Nov 19, 2021)

    This is the first minor release of ticktock. It contains many bug fixes and a major overhaul of the formatting system.

    • The format string now can control the whole output line (including the beginning of the line with the name). Format can be set globally with set_format.
    • Providing a format=... keyword argument to any of the ticktock functions (decorator, context manager, tick/tock) will override the formatting for this clock.
    • Format strings now accept a generic name parameter that takes into account the provided name parameters to ticktock functions, and has a consistent behavior across use cases
    • tick returns a Clock instance, while tock returns the recorded time delta in ns
    • clock disambiguation now exclusively occurs with frame information (also used names previously)
    • Internal modules have been reorganized: ticktock.renderers has been split into ticktock.renderers and ticktock.std , ticktock.timer has been split in ticktock.clocks, ticktock.collection and ticktock.timers
    Source code(tar.gz)
    Source code(zip)
  • v0.0.7(Nov 14, 2021)

    Easier control on formatting: use set_format(format: str, max_terms: int = None, no_update: bool) to set:

    • the format string format
    • the number of displayed units it times max_terms (https://github.com/victorbenichoux/ticktock/issues/6)
    • and to turn off ASCII control characters (used to update the previous line) with no_update This was signaled by https://github.com/victorbenichoux/ticktock/issues/4.

    Turn on or off the ticktock collection of timings and rendering with enable/disable. (https://github.com/victorbenichoux/ticktock/issues/5)

    Fix a bug in which the carriage return was not returned.

    Improvements to the documentation, some more testing.

    Thanks to all the people who contributed issues!

    Source code(tar.gz)
    Source code(zip)
Owner
Victor Benichoux
Principal data scientist/ex- computational neuroscientist
Victor Benichoux
DiddiParser 2: The DiddiScript parser.

DiddiParser 2 The DiddiScript parser, written in Python. Installation DiddiParser2 can be installed via pip: pip install diddiparser2 Usage DiddiPars

Diego Ramirez 3 Dec 28, 2022
Script to autocompound 3commas BO:SO based on user provided risk factor

3commas_compounder Script to autocompound 3commas BO:SO based on user provided risk factor Setup Step 1 git clone this repo into your working director

0 Feb 24, 2022
Python Yeelight YLKG07YL/YLKG08YL dimmer handler

With this class you can receive, decrypt and handle Yeelight YLKG07YL/YLKG08YL dimmer bluetooth notifications in your python code.

12 Dec 26, 2022
Python lightweight dependency injection library

pythondi pythondi is a lightweight dependency injection library for python Support both sync and async functions Installation pip3 install pythondi Us

Hide 41 Dec 16, 2022
This utility lets you draw using your laptop's touchpad on Linux.

FingerPaint This utility lets you draw using your laptop's touchpad on Linux. Pressing any key or clicking the touchpad will finish the drawing

Wazzaps 95 Dec 17, 2022
Tools for binary data on cassette

Micro Manchester Tape Storage Tools for storing binary data on cassette Includes: Python script for encoding Arduino sketch for decoding Eagle CAD fil

Zack Nelson 28 Dec 25, 2022
Audio Steganography is a technique used to transmit hidden information by modifying an audio signal in an imperceptible manner.

Audio Steganography Audio Steganography is a technique used to transmit hidden information by modifying an audio signal in an imperceptible manner. Ab

Karan Yuvraj Singh 1 Oct 17, 2021
Find unused resource keys in properties files in a Salesforce Commerce Cloud project and get rid of them.

Find Unused Resource Keys Find unused resource keys in properties files in a Salesforce Commerce Cloud project and get rid of them. It looks through a

Noël 5 Jan 08, 2022
A small python library that helps you to generate localization strings for your mobile projects.

LocalizationUtiltiy A small python library that helps you to generate localization strings for your mobile projects. This small script aims to help yo

1 Nov 12, 2021
.bvh to .mcfunction file converter.

bvh-to-mcf .bvh file to .mcfunction converter

Hanmin Kim 28 Nov 21, 2022
Utility to play with ADCS, allows to request tickets and collect information about related objects.

certi Utility to play with ADCS, allows to request tickets and collect information about related objects. Basically, it's the impacket copy of Certify

Eloy 185 Dec 29, 2022
A script to check for common mistakes in LaTeX source files of scientific papers.

LaTeX Paper Linter This script checks for common mistakes in LaTeX source files of scientific papers. Usage python3 paperlint.py file.tex [-i/x inc

Michael Schwarz 12 Nov 16, 2022
python script to generate color coded resistor images

Resistor image generator I got nerdsniped into making this. It's not finished at all, and the code is messy. The end goal it generate a whole E-series

MichD 1 Nov 12, 2021
A module for account creation with python

A module for account creation with python

Fayas Noushad 3 Dec 01, 2021
Genart - Generate random art to sell as nfts

Genart - Generate random art to sell as nfts Usage git clone

Will 13 Mar 17, 2022
Aurin - A quick AUR installer for Arch Linux. Install packages from AUR website in a click.

Aurin - A quick AUR installer for Arch Linux. Install packages from AUR website in a click.

Suleman 51 Nov 04, 2022
Lark is a parsing toolkit for Python, built with a focus on ergonomics, performance and modularity.

Lark is a parsing toolkit for Python, built with a focus on ergonomics, performance and modularity.

Lark - Parsing Library & Toolkit 3.5k Jan 05, 2023
A random cats photos python module

A random cats photos python module

Fayas Noushad 6 Dec 01, 2021
Simple RGB to HEX game made in python

Simple RGB to HEX game made in python

5 Aug 26, 2022
cpp20.py is a Python script to compile C++20 code using modules.

cpp20.py is a Python script to compile C++20 code using modules. It browses the source files to determine their dependencies. Then, it compiles then in order using the correct flags.

Julien VERNAY 6 Aug 26, 2022