Skip to main content

A package that allows timing of each individual line of code in a function, and produces a colour-coded output

Project description

Deep time-it

Deep time-it is an open source module that was intended to be an extension of the timeit library, that not only times a function, but can also time each individual line and chunk of code within it, and produce a visual break-down of the slower and faster parts to aid with debugging and refactoring.

Important

Be careful if your function that you are timing has side effects. If it does, then as sometimes the function may be run twice, the side effect might occur twice. To disable double running, set the maxrepeats flag to None. This is done be default.

Installation

To install the Deep time-it library, use pip install deep_timeit. The documentation can be found at TODO. We support Python versions 3.6 to 3.10.

Usage

Run deep_timeit.deepTimeit(function), and replace function with a reference to the function you want to time. It includes the additional flags args and kwargs, which you can set to a list and dictionary respectively that includes additional arguments and keyword arguments to include when timing the function. The other flags are maxrepeats, which if set to an integer, which it is by default, will check if any lines are run more than the threshold and in which case will reattempt timing the function without timing those particular lines. This is a feature because if it times a line a large number of times, the timing itself will start to contribute to the time taken. The function returns an Info object, which can be displayed by running infoob.show().

Examples

Example 1

import deep_timeit
import time

def add(a, b):
    accumilator = 0
    for i in range(a):
      accumilator += 1
    for i in range(b):
      accumilator += 1
    print(f"The result of the addition of a and b is: {accumilator}")

deep_timeit.deepTimeit(add, args=[100000, 200000]).show()

Example 2

from deep_timeit import deepTimeit

def incrementalTime():
    """This is a function that takes incrementally long to run
    and is a good test of the range of colours."""
    for i in range(4):
        time.sleep(0.1)
        time.sleep(0.2)
        time.sleep(0.3)
        time.sleep(0.4)
        time.sleep(0.5)
        time.sleep(0.6)
        time.sleep(0.7)
        time.sleep(0.8)
        time.sleep(0.9)
        time.sleep(1)
    for i in range(2):
        time.sleep(0.1)
        time.sleep(0.2)
        time.sleep(0.3)
        time.sleep(0.4)
        time.sleep(0.5)
        time.sleep(0.6)
        time.sleep(0.7)
        time.sleep(0.8)
        time.sleep(0.9)
        time.sleep(1)
        for j in range(3):
            time.sleep(0.01)
            time.sleep(0.02)
            time.sleep(0.03)
            time.sleep(0.04)
            time.sleep(0.05)
            time.sleep(0.06)
            time.sleep(0.07)
            time.sleep(0.08)
            time.sleep(0.09)
            time.sleep(0.1)

deepTimeit(incrementalTime).show()

Example 3

from deep_timeit import deepTimeit, Colour
import random

deepTimeit(random._test).show()

Example 4

from deep_timeit import deepTimeit

def incrementalTime():
    """This is a function that takes incrementally long to run
    and is a good test of the range of colours."""
    for i in range(4):
        time.sleep(0.1)
        time.sleep(0.2)
        time.sleep(0.3)
        time.sleep(0.4)
        time.sleep(0.5)
        time.sleep(0.6)
        time.sleep(0.7)
        time.sleep(0.8)
        time.sleep(0.9)
        time.sleep(1)
    for i in range(2):
        time.sleep(0.1)
        time.sleep(0.2)
        time.sleep(0.3)
        time.sleep(0.4)
        time.sleep(0.5)
        time.sleep(0.6)
        time.sleep(0.7)
        time.sleep(0.8)
        time.sleep(0.9)
        time.sleep(1)
        for j in range(3):
            time.sleep(0.01)
            time.sleep(0.02)
            time.sleep(0.03)
            time.sleep(0.04)
            time.sleep(0.05)
            time.sleep(0.06)
            time.sleep(0.07)
            time.sleep(0.08)
            time.sleep(0.09)
            time.sleep(0.1)

deepTimeit(incrementalTime).show(colourrange=ColourRange.RAINBOW)

Release notes

Github releases

Similar libraries

  • timeit
  • line_profiler
  • pprofile

TODO's

Make code work with functions that have:

  • Multiline brackets

Make code work with:

  • Class methods
  • Recursive functions

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

deep_timeit-1.1.1.tar.gz (623.0 kB view hashes)

Uploaded Source

Built Distribution

deep_timeit-1.1.1-py3-none-any.whl (12.0 kB view hashes)

Uploaded Python 3

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