Skip to main content

A small example package

Project description

TicTac - Time your Torch Code Easily

This repository simplifies timing of code by reducing the amount of boilerplate code needed. Specifically, it provides a context manager that can be used to time a block of code. The context manager will print the time taken to execute the block of code. Additionally, full classes/objects can be timed easily and statistics are generated.

Installation

pip3 install pytictac

Basic Usage

Old:

import torch

def func(x):
    return x*2

start = torch.cuda.Event(enable_timing=True)
end = torch.cuda.Event(enable_timing=True)
start.record()

# Compute
func(2)

end.record()
torch.cuda.synchronize()
print(start.elapsed_time(end))

New:

from pytictac import Timer

def func(x):
    return x*2

# Compute
with Timer("func2"):
    func(2)

Terminal Output:

Time func2:  0.0655359998345375 ms

Advanced Usage

from pytictac import ClassTimer, ClassContextTimer, accumulate_time


class SmallObject:
    def __init__(self):
        self.x = 3

    @accumulate_time
    def method_a(self):
        self.x += 2


class TestObject:
    def __init__(self):
        self.x = 3
        self.small_obj = SmallObject()

        self.cct = ClassTimer(objects=[self, self.small_obj], names=["Test Object", "Small Object"], enabled=True)

    @accumulate_time
    def method_a(self):
        self.x += 2
        self.small_obj.method_a()

    @accumulate_time
    def method_b(self):
        self.x += 2
        with ClassContextTimer(parent_obj=self, block_name="method_b.1", parent_method_name="method_b"):
            self.x = 3
            with ClassContextTimer(
                parent_obj=self, block_name="method_b.1.a", parent_method_name="method_b.1", n_level=2
            ):
                self.x = 4

    @accumulate_time
    def method_c(self):
        self.x -= 2

    @accumulate_time
    def method_d(self):
        self.x = 0

    def __str__(self):
        return self.cct.__str__()


to = TestObject()
to.method_a()
to.method_a()
to.method_a()
to.method_b()
to.method_c()
print(to)

Terminal Output:

Test Object                           total [ms]    count [n]        std [ms]       mean [ms]
  +-  method_a:                       0.16          3                0.045          0.054           
  +-  method_b:                       0.05          1                0.0            0.05            
  +------  method_b.1:                0.03          1                0.0            0.027           
  +-----------  method_b.1.a:         0.01          1                0.0            0.007           
  +-  method_c:                       0.01          1                0.0            0.005           
Small Object                          total [ms]    count [n]        std [ms]       mean [ms]
  +-  method_a:                       0.02          3                0.001          0.005       

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytictac-1.0.2-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

Details for the file pytictac-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: pytictac-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 5.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.15

File hashes

Hashes for pytictac-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 a83d5cae56273be64f2d8945a725edd80ae94c0e169a9facf0834b38f14abd40
MD5 9be09a7418d9ea529eb25609cf02f01f
BLAKE2b-256 ffad03ed26cbc626ab7f3acad98f0948043e406e845ec364c822eb8feaedce28

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page