Skip to main content

Mini Profiler & Analyzer for Python Functions

Project description

algo_analyze

Mini Profiler & Analyzer for Python Functions

algo_analyze is a Python package and command-line tool that helps developers analyze their Python functions and scripts. It measures execution time, memory usage, function call counts, and exceptions, providing a clean, readable report. Perfect for debugging, performance profiling, and algorithm analysis.


Features

  • Measure execution time of Python functions (seconds or milliseconds)
  • Track memory usage (current and peak)
  • Count how many times a function is called
  • Log exceptions with full traceback
  • Command-line interface (CLI) for profiling functions or scripts
  • Supports single or multiple scripts/functions
  • Tabular summary for multiple scripts
  • Lightweight and easy to integrate

Installation

Using pip:

pip install algo_analyze

Install from source:

git clone https://github.com/prachi-33/algo_analyze.git
cd algo_analyze
pip install -e .

Usage

1. Python Decorator

Use @inspect to profile a function:

from algo_analyze import inspect

@inspect
def add(a, b):
    return a + b

report = add(5, 10)
print(report)

Sample Output:

Function: add
----------------------------------------
Execution Time : 0.000123 sec
Memory Used    : 3.12 KB
Peak Memory    : 5.45 KB
Call Count     : 1
Exception      : None
----------------------------------------
Return Value   : 15

2. Command-Line Interface (CLI)

Profile any Python function or script from the terminal:

algo_analyze <script.py> [function_name] [args...]

Examples:

Profile a specific function:

algo_analyze my_script.py my_function arg1 arg2

Profile an entire script:

algo_analyze my_script.py

Profile multiple scripts:

algo_analyze script1.py script2.py script3.py

Sample CLI Output:

Analyzing: my_script.py::my_function
========================================
Function: my_function
----------------------------------------
Execution Time : 1.234 sec
Memory Used    : 15.67 KB
Peak Memory    : 23.45 KB
Call Count     : 1
Exception      : None
----------------------------------------

3. Advanced Usage

Profile with custom time units:

from algo_analyze import inspect

@inspect(time_unit='ms')
def complex_calculation():
    return sum(range(1000000))

report = complex_calculation()
print(report)

Profile recursive functions:

from algo_analyze import inspect

@inspect
def fibonacci(n):
    if n <= 1:
        return n
    return fibonacci(n-1) + fibonacci(n-2)

report = fibonacci(10)
print(report)
# Call count will show total recursive calls

Context manager usage:

from algo_analyze import Profiler

with Profiler() as prof:
    # Your code here
    result = some_function()
    
print(prof.report())

Configuration

Decorator Options

@inspect(
    time_unit='sec',    # 'sec' or 'ms'
    memory_unit='KB',   # 'KB', 'MB', or 'GB'
    show_traceback=True # Show full traceback on exception
)
def my_function():
    pass

CLI Options

algo_analyze --help                    # Show help
algo_analyze --version                 # Show version
algo_nalyze script.py --json          # Output as JSON
algo_analyze script.py --csv           # Output as CSV
algo_analyze script.py --verbose       # Detailed output

Examples

Example 1: Analyzing Algorithm Performance

from algo_analyze import inspect

@inspect
def bubble_sort(arr):
    n = len(arr)
    for i in range(n):
        for j in range(0, n-i-1):
            if arr[j] > arr[j+1]:
                arr[j], arr[j+1] = arr[j+1], arr[j]
    return arr

data = [64, 34, 25, 12, 22, 11, 90]
report = bubble_sort(data.copy())
print(report)

Example 2: Memory-Intensive Operations

from algo_analyze import inspect

@inspect
def create_large_list():
    return [i**2 for i in range(1000000)]

report = create_large_list()
print(report)

Example 3: Exception Handling

from algo_analyze import inspect

@inspect
def divide(a, b):
    return a / b

report = divide(10, 0)  # Will catch and report ZeroDivisionError
print(report)

API Reference

@inspect Decorator

Decorator to profile a function.

Parameters:

  • time_unit (str): Time unit for display ('sec' or 'ms')
  • memory_unit (str): Memory unit for display ('KB', 'MB', 'GB')
  • show_traceback (bool): Show exception traceback

Returns: Function wrapper that returns profiling report

Profiler Class

Context manager for profiling code blocks.

Methods:

  • report(): Get profiling report as string
  • stats(): Get raw statistics as dictionary

Roadmap

  • Add visualization support (graphs/charts)
  • Support for async functions
  • Integration with popular testing frameworks
  • Web dashboard for real-time monitoring
  • Comparative analysis between runs
  • Export to multiple formats (HTML, PDF)

Contributing

Contributions are welcome! Please follow these steps:

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

Please ensure your PR:

  • Follows PEP 8 style guidelines
  • Includes tests for new features
  • Updates documentation as needed

Support


Acknowledgments

  • Inspired by Python's built-in timeit and tracemalloc modules
  • Thanks to all contributors and users

Author

Prachi


Star this repo if you find it useful!

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

algo_analyze-0.1.0.tar.gz (7.0 kB view details)

Uploaded Source

Built Distribution

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

algo_analyze-0.1.0-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

Details for the file algo_analyze-0.1.0.tar.gz.

File metadata

  • Download URL: algo_analyze-0.1.0.tar.gz
  • Upload date:
  • Size: 7.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.9

File hashes

Hashes for algo_analyze-0.1.0.tar.gz
Algorithm Hash digest
SHA256 474f2a4c83d064fedf5e02591c16b55bb4eed9bed18682c98335fd1adff30b8f
MD5 d2935528ab574bd1cb5d129155c1ab92
BLAKE2b-256 530c6fe87996be4212c1e4826ebf38bdd41e377347f95bb1232af6b0ddef42c4

See more details on using hashes here.

File details

Details for the file algo_analyze-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: algo_analyze-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.9

File hashes

Hashes for algo_analyze-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e825e9f092c042308ea15242096a40f5029a6a4559253d93c66b34ef8af436f9
MD5 d708f850fdc2072ae5894df548747c95
BLAKE2b-256 7123bc93ea26b32c61e4fd69fc3fc0c21c26f68a6297a59c8640a27179803387

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