Skip to main content

A flexible profiling utility for Python.

Project description

banner

Python Version PyPI License PyPI Version

🚀 About

FlexProfiler is a lightweight, flexible profiling utility for Python that makes it easy to measure function and method performance with minimal code changes. It supports both function-level and line-level profiling, recursive class method tracking, and customizable statistics reporting. Designed for developers who want actionable insights into their code’s runtime behavior, FlexProfiler helps you identify bottlenecks and optimize performance with simple decorators and clear output.

📦 Installation

pip install flexprofiler

💻 Usage

The most common usage involves adding simple decorators to your functions or classes, enabling you to profile execution time and other metrics with minimal changes to your codebase. This approach makes it easy to identify bottlenecks and optimize your code efficiently.

  • @track() - decorate a function to collect call counts and timing statistics. Args:
    • lines: boolean, collect per-line timing inside the function.
  • @track() - Decorate a class to profile all its methods. Args:
    • max_depth: integer, how deep to recursively profile composed objects (default: 10).
    • include: list of method names to track (default: None to track all).
    • exclude: list of method names to skip (default: None to skip none).
    • arg_sensitive: list of method names to track argument values (default: None).
  • stats() - print aggregated profiling statistics (function-level and, when enabled, line-level). Args:
    • unit: the time unit to use for displaying statistics (default: "ms").

⬇️ Examples

1. Tracking a Simple Function

Simple function profiling example — demonstrates using @track to profile a standalone function.

import time
from flexprofiler import stats, track

@track()  # Use @track() decorator to profile the function
def simple_func():
    time.sleep(0.1)

simple_func()
simple_func()

stats() # display the profiling statistics

output: flexprofiler command output screenshot


2. Tracking All Methods in a Class

Class profiling example — demonstrates using @track to profile all methods of a class.

import time
from flexprofiler import track, stats

@track()  # Use @track() decorator to profile all methods in the class
class Foo:
    def example_method(self):
        self.another_method()
        time.sleep(0.1)
    def another_method(self):
        time.sleep(0.2)

Foo().example_method()
Foo().another_method()

stats()

output: flexprofiler command output screenshot


3. Tracking All Methods in a Class Recursively

Recursive class profiling example — demonstrates recursively tracking methods in nested/instantiated classes using @track(max_depth=...).

import time
from flexprofiler import track, stats

@track(max_depth=3)
class Foo:
    def __init__(self):
        self.sub_class = Bar()
    def example_method(self):
        self.another_method()
        time.sleep(0.1)
    def another_method(self):
        time.sleep(0.2)
    def calling_subclass_method(self):
        for i in range(3):
            self.sub_class.subclass_method_1()
            self.sub_class.subclass_method_2()
            self.sub_class.subclass_method_3()

class Bar:
    def subclass_method_1(self):
        time.sleep(0.05)
    def subclass_method_2(self):
        self.a()
        self.b()
    def subclass_method_3(self):
        self.a()
        self.b()
    def a(self):
        time.sleep(0.02)
    def b(self):
        time.sleep(0.01)

obj = Foo()
obj.example_method()
obj.calling_subclass_method()

stats()

output: flexprofiler command output screenshot


4. Track All lines in a function

Line-by-line profiling example — demonstrates per-line timing with @track(lines=True).

import time
from flexprofiler import track, stats


@track(lines=True)  # with line tracking
def bar(n):
    total = 0
    for i in range(n):
        total += i
        if i % 2 == 0:
            time.sleep(0.001)
        else:
            time.sleep(0.0005)
    return total

@track()  # no line tracking
def baz():
    time.sleep(0.1)

@track()  # no line tracking
def foo():
    for _ in range(3):
        bar(50)
    baz()

foo()
stats()  # display the profiling statistics

output: flexprofiler command output screenshot


🤝 Feedback and Contributions

Contributions, suggestions, and feedback are welcome! Feel free to open issues or submit pull requests on GitHub.


Author

Arthur Fender C Bucker

website

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

flexprofiler-0.1.5.tar.gz (415.4 kB view details)

Uploaded Source

Built Distribution

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

flexprofiler-0.1.5-py3-none-any.whl (13.0 kB view details)

Uploaded Python 3

File details

Details for the file flexprofiler-0.1.5.tar.gz.

File metadata

  • Download URL: flexprofiler-0.1.5.tar.gz
  • Upload date:
  • Size: 415.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.11.11

File hashes

Hashes for flexprofiler-0.1.5.tar.gz
Algorithm Hash digest
SHA256 d9221511a30917dec3a3e0fdefe2187ff70eb20668728825751a3c4c55fb86ad
MD5 25426ac08ad5a9cb699fff0c96bb26d4
BLAKE2b-256 6a5ba5e6669e32598e3457a340555921f8f1357d003de256189ba03f45c76f0e

See more details on using hashes here.

File details

Details for the file flexprofiler-0.1.5-py3-none-any.whl.

File metadata

  • Download URL: flexprofiler-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 13.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.11.11

File hashes

Hashes for flexprofiler-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 55549e2c767c8295307b6ff23591fda2b3e2354ebb20c77a0d6c1827fdf7e19f
MD5 a7232ee690d62ad4f447f37063e7d13e
BLAKE2b-256 2c0a3b342471d74d4caaff3afca663f5188205a09d06c2441c098f272080ba95

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