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, CpuTimer

def func(x):
    return x*2

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

with CpuTimer("func2"):
    func(2)

Terminal Output:

Time func2:  0.0655359998345375 ms

There exists always a cpu version (using import time; st = time.time()) and a gpu-version (using torch events). This is handy when you want to time for example a dataloader where you cannot use torch events.

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, cpu=True
            ):
                self.x = 4

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

    @cpu_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       

Releasing

python3 setup.py bdist_wheel 
twine upload dist/* 

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.5-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pytictac-1.0.5-py3-none-any.whl
  • Upload date:
  • Size: 5.4 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.5-py3-none-any.whl
Algorithm Hash digest
SHA256 8c2aeec711a6ce3862f24c150851e3cc6999cae38e07c802808caa5d9e02766d
MD5 5fb9edafbe49a9f3ebb5011875a3167c
BLAKE2b-256 5ea63c20c1eed3b0a40fb5c089a76f1ddd7a0f1b442f1b39937f23331af166a2

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