Lightweight profiler with checkpoints
Project description
sProfiler
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file sprofiler-0.1.0.tar.gz
.
File metadata
- Download URL: sprofiler-0.1.0.tar.gz
- Upload date:
- Size: 3.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/46.0.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.6.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | abae3c356cbe107d5eb1972dc24270ca24f0d14454fc2f2148fc44bf445d1a33 |
|
MD5 | 4e2ff3f8837941e212575265649113a5 |
|
BLAKE2b-256 | 467ddc203f4747939ee2ad7ccd3ea80349e15f4657de4096df70dc58b5eec490 |
File details
Details for the file sprofiler-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: sprofiler-0.1.0-py3-none-any.whl
- Upload date:
- Size: 4.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/46.0.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.6.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | fad413920f85edb5b3296ced7aeada086f9d91bb91b295c089de6daf3168f730 |
|
MD5 | ede3e1eda42a5353d02ff2ac3fcf6418 |
|
BLAKE2b-256 | 600a81f95988f54f64558f2422128668df5d4377faeeb3f63bee9a068545cb51 |