Automatic time complexity analysis with visualization for Python functions
Project description
SpeedSense
SpeedSense is a Python package that automatically analyzes and determines the time complexity of Python functions. It uses static code analysis and curve fitting techniques to provide accurate complexity estimates with optional visualization capabilities.
Features
- Automatic Complexity Analysis: Analyzes Python functions and determines their time complexity
- Visualization Support: Optional plotting of complexity curves with smooth interpolation
- Multiple Complexity Models: Supports O(1), O(log n), O(n), O(n log n), O(n²), and O(n³) analysis
- Code Instrumentation: Automatically instruments loops to count iterations
- Flexible Input: Works with both function objects and source code strings
Limitations
Currently, the library works with:
- Single-variable input functions (functions that take one parameter)
- Non-recursive functions (no recursive calls)
- Basic loop structures (for and while loops)
- Simple arithmetic operations
Note: Built-in library functions and complex data structures may not be fully supported yet.
Installation
Use the package manager pip to install speedsense:
pip install speedsense
Usage
Basic Usage
from speedsense.tc_estimator import get_time_complexity
import inspect
def my_function(n):
for i in range(n):
for j in range(n):
pass # Some operation
# Method 1: Using function object directly
complexity = get_time_complexity(my_function, [10, 15, 20, 25, 30])
print(f"Time complexity: {complexity}")
# Method 2: Using source code
source_code = inspect.getsource(my_function)
complexity = get_time_complexity(source_code, [10, 15, 20, 25, 30])
print(f"Time complexity: {complexity}")
With Visualization
from speedsense.tc_estimator import get_time_complexity
import 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]
# Get complexity with visualization
complexity, fig, ax = get_time_complexity(
bubble_sort,
[10, 15, 20, 25, 30],
visualize=True
)
print(f"Time complexity: {complexity}")
# Save the plot
complexity, fig, ax = get_time_complexity(
bubble_sort,
[10, 15, 20, 25, 30],
visualize=True,
save_path="complexity_plot.png"
)
Using compute_complexity (Legacy Method)
from speedsense.tc_estimator import compute_complexity
import inspect
def userfunc(n):
for i in range(n):
for j in range(n):
pass # Some operation
code = inspect.getsource(userfunc)
compute_complexity(code) # prints the time complexity
# With visualization
compute_complexity(code, visualize=True)
API Reference
get_time_complexity(func, input_sizes, visualize=False, show=True, save_path=None)
Analyzes the time complexity of a function.
Parameters:
func: Function object or source code string to analyzeinput_sizes: List of input sizes to test (e.g., [10, 15, 20, 25, 30])visualize: Boolean, whether to create a visualization plot (default: False)show: Boolean, whether to display the plot (default: True)save_path: String, optional path to save the plot image
Returns:
- If
visualize=False: Returns the complexity string (e.g., "O(n²)") - If
visualize=True: Returns tuple (complexity, figure, axes)
compute_complexity(source_code, visualize=False)
Legacy function for analyzing source code strings.
Parameters:
source_code: String containing the function source codevisualize: Boolean, whether to create a visualization plot
Examples
Linear Time Complexity
def linear_function(n):
for i in range(n):
pass # O(n) operation
complexity = get_time_complexity(linear_function, [10, 20, 30, 40, 50])
# Output: O(n)
Quadratic Time Complexity
def quadratic_function(n):
for i in range(n):
for j in range(n):
pass # O(n²) operation
complexity = get_time_complexity(quadratic_function, [5, 10, 15, 20, 25])
# Output: O(n²)
Logarithmic Time Complexity
def log_function(n):
i = n
while i > 1:
i = i // 2 # O(log n) operation
complexity = get_time_complexity(log_function, [10, 20, 40, 80, 160])
# Output: O(log n)
How It Works
- Code Instrumentation: The library automatically instruments your function by adding counters to loops
- Execution: Runs the instrumented function with different input sizes
- Data Collection: Collects iteration counts for each input size
- Curve Fitting: Fits the data to different complexity models using least squares regression
- Analysis: Determines the best-fitting complexity model
- Visualization: Optionally creates plots showing the data points and fitted curve
Contributing
Contributions are welcome! Please feel free to submit pull requests or open issues for bugs and feature requests.
License
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file speedsense-0.2.2.tar.gz.
File metadata
- Download URL: speedsense-0.2.2.tar.gz
- Upload date:
- Size: 10.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
291b97f4ad2d8023f8421f5ce7ba26777c6d64035130dd4874987d069efbefe2
|
|
| MD5 |
0cc678b4359bdf449ed397a0f7a62e2d
|
|
| BLAKE2b-256 |
f5c611ce73f7784f3775435ddbb7c57e1e1e5fc0a8a93bbfefac12d287fd40b8
|
File details
Details for the file speedsense-0.2.2-py3-none-any.whl.
File metadata
- Download URL: speedsense-0.2.2-py3-none-any.whl
- Upload date:
- Size: 8.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d8f504bd5f8dd54cf7e23526d152e3feaf90ca6485821bc7264017448c29403f
|
|
| MD5 |
c5082cf3514fddc0d64f7b12c2bcd727
|
|
| BLAKE2b-256 |
53d5e4a7ad813d6f1ae1cbd2bd7f7421eaeb19b4ea31d5dc81eaead74575ff9e
|