Skip to main content

A tool to analyze time and space complexity of Python code with visualizations

Project description

Complexity Analyzer

A Python module to analyze time and space complexity of functions using static analysis and runtime profiling, with visualization via graphs.

Features

  • Static Analysis: Infers complexity from code structure (e.g., loops, recursion).
  • Profiling: Measures actual time and memory usage across input sizes.
  • Visualization: Plots time and space complexity graphs using Matplotlib.
  • Command-Line Interface: Analyze algorithms directly from the terminal.

Installation

pip install complexity-analyzer

Quick Start

from complexity_analyzer import complexity

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

# Run with sample data
data = [5, 3, 8, 6, 7, 2]
sorted_data = bubble_sort(data)
# The decorator will automatically:
# 1. Print the static analysis complexity
# 2. Run profiling with varying input sizes
# 3. Display complexity graphs

Usage

As a Decorator

The simplest way to use Complexity Analyzer is as a decorator:

from complexity_analyzer import complexity

@complexity
def your_function(input_data):
    # Your algorithm here
    return result

Manual Analysis

You can also analyze code directly:

from complexity_analyzer import analyze_code

code = """
def binary_search(arr, target):
    left, right = 0, len(arr) - 1
    while left <= right:
        mid = (left + right) // 2
        if arr[mid] == target:
            return mid
        elif arr[mid] < target:
            left = mid + 1
        else:
            right = mid - 1
    return -1
"""

result = analyze_code(code)
print(result)  # Outputs: "Time Complexity: O(log n), Space Complexity: O(1)"

Advanced Usage

Custom Input Sizes

You can customize the input sizes used for profiling:

from complexity_analyzer import complexity

@complexity(input_sizes=[10, 50, 100, 500, 1000])
def your_function(input_data):
    # Your algorithm here
    return result

Disable Visualization

If you don't want to display the graphs automatically:

from complexity_analyzer import complexity

@complexity(visualize=False)
def your_function(input_data):
    # Your algorithm here
    return result

Silent Mode

For silent operation without any output:

from complexity_analyzer import complexity

@complexity(verbose=False, visualize=False)
def your_function(input_data):
    # Your algorithm here
    return result

Command-Line Interface

The package provides a command-line interface for quick analysis of Python code:

# Analyze code directly
complexity-analyzer --code "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"

# Analyze a specific function in a file
complexity-analyzer --file my_algorithm.py --function bubble_sort

# Run and profile a function
complexity-analyzer --file my_algorithm.py --function bubble_sort --run

# Run with custom input sizes and save the plot
complexity-analyzer --file my_algorithm.py --function bubble_sort --run --input-sizes 50 500 5000 --save-plot complexity_plot.png

# Analyze all functions in a file
complexity-analyzer --file my_algorithm.py --analyze-all

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

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

complexity_analyzer-0.1.0.tar.gz (15.6 kB view details)

Uploaded Source

Built Distribution

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

complexity_analyzer-0.1.0-py3-none-any.whl (16.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: complexity_analyzer-0.1.0.tar.gz
  • Upload date:
  • Size: 15.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for complexity_analyzer-0.1.0.tar.gz
Algorithm Hash digest
SHA256 e3566045b025b5fa33dde5294fe6fb687a2e8c30f4251a5b836adad7422e58cd
MD5 6b3ac6811bd910c95e0a0fd0a82a842f
BLAKE2b-256 e5a45de654fb3dadb8f27f3fdb3ddbcbeb29bd45ed4013b203c791d5916f2e30

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for complexity_analyzer-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 28c1bbf24816485cae188fc1ebf273530b4222dd0724ab92d0ad7c3f2cd85dea
MD5 945482bf42fbf8f8512adbf042800565
BLAKE2b-256 9d32dec0a4f5a67993fdb11e62315642f47e8ed9c5b4c93b4b30af3fe3a98c26

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