Skip to main content

Lightweight profiler with checkpoints

Project description

sProfiler

PyPI version

Python script profiler/timer supporting code checkpoints and reporting.

Installation

pip install sprofiler

Usage

Use the Profiler to create named checkpoints throughout your code. Checkpoints need a 'start' and a 'stop', and multiple iterations are combined to summarize how long it takes to complete each leg. The report() function prints the results from all checkpoints.

import sprofiler as sp
from time import sleep

pr = sp.Profiler()

pr.start('program')
print('Code outside loop')
sleep(1)

for _ in range(10):
    pr.start('sleep_1s')
    print('Code in loop')
    sleep(1)
    pr.stop('sleep_1s')
pr.stop('program')

pr.report()

The printed report appears as:

program | 11.0 s ± 0.0 s per iteration, n = 1
sleep_1s | 1.0 s ± 0.0 s per iteration, n = 10

Logging

sProfiler automatically logs results by default. You can change the destination filename, as well as the level of verbosity: 0 - no logging, 1 - only elapsed times, 2 - start and stop times. Defaults are logname='profiler.log' and verbose=2.

import sprofiler as sp
from time import sleep

pr = sp.Profiler(logname='my_log.log', verbose=1)

Function Decorators

sProfiler also supports timing functions with decorators, via Profiler.time_func. Demonstrating on the first example:

pr = sp.Profiler()

@pr.time_func
def sleep_1s():
    print('Code in loop')
    sleep(1)

@pr.time_func
def my_func():
    for _ in range(10):
        sleep_1s()

pr.report()

The printed report appears as:

my_func | 10.0 s ± 0.0 s per iteration, n = 1
sleep_1s | 1.0 s ± 0.0 s per iteration, n = 10

Future Directions

  • Potential support for more complex profiling

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

sprofiler-0.1.0.tar.gz (3.5 kB view hashes)

Uploaded Source

Built Distribution

sprofiler-0.1.0-py3-none-any.whl (4.8 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