Python time complexity analysis library using AST parsing
Project description
tc-analyzer
A Python library for analyzing time complexity of code through AST parsing.
Installation
pip install tc-analyzer
Usage
from tc_analyzer import analyze
# Analyze code string
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
'''
result = analyze(code)
print(result.functions[0].complexity.overall) # O(n^2)
print(result.functions[0].algorithm_type) # sorting
# Analyze file
result = analyze('/path/to/your/file.py')
# Analyze specific function
result = analyze(code, target='function:my_func')
Features
- Loop nesting detection (O(n), O(n^2), O(n^3), etc.)
- Built-in function complexity (sorted, max, len, etc.)
- Data structure operation complexity (list.append, dict.get, etc.)
- Recursion analysis (linear, divide-and-conquer, exponential)
- Algorithm pattern recognition (sorting, search, dynamic programming)
- Line-by-line complexity contributions
Output Structure
class FunctionAnalysis:
name: str # Function name
complexity: ComplexityResult # O(n) with confidence
lines: List[LineContribution] # Line-by-line breakdown
loops: int # Nesting depth
recursion: bool # Is recursive
algorithm_type: str # 'sorting', 'search', 'dp', etc.
Supported Complexity Patterns
| Pattern | Detected |
|---|---|
| Nested loops | O(n^k) |
| Built-in sorted | O(n log n) |
| Linear recursion | O(n) |
| Binary recursion | O(2^n) |
| Divide by 2 recursion | O(log n) |
Examples
Loop Detection
code = '''
def process(arr):
for i in arr:
for j in arr:
print(i, j)
'''
result = analyze(code)
# Output: O(n^2) - 2 nested loops detected
Built-in Function
code = '''
def sort_list(arr):
return sorted(arr)
'''
result = analyze(code)
# Output: O(n log n) - built-in function with O(n log n)
Recursion
code = '''
def factorial(n):
if n <= 1:
return 1
return n * factorial(n - 1)
'''
result = analyze(code)
# Output: O(n) - recursive function with O(n) complexity
Algorithm Recognition
code = '''
def binary_search(arr, low, high, x):
if high >= low:
mid = (low + high) // 2
if arr[mid] == x:
return mid
...
'''
result = analyze(code)
# Output: algorithm_type = 'search'
License
MIT
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
tc_analyzer-0.1.0.tar.gz
(15.6 kB
view details)
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 tc_analyzer-0.1.0.tar.gz.
File metadata
- Download URL: tc_analyzer-0.1.0.tar.gz
- Upload date:
- Size: 15.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a7938e435e0aebd3c03bd0f9ff58351cf6f3348b439d46d330c93e3aca7b5a4b
|
|
| MD5 |
f1c31280060a3e17acfd37499327a887
|
|
| BLAKE2b-256 |
f29ad27d02a678ce2534ee3b4c9a9a6b350bcc15df19cc3bf84e8342e95c7f3e
|
File details
Details for the file tc_analyzer-0.1.0-py3-none-any.whl.
File metadata
- Download URL: tc_analyzer-0.1.0-py3-none-any.whl
- Upload date:
- Size: 14.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7f9a4d4ed4ad5b0aca1305e00d2849a3f9a36c916e3fa2f47cc7737b90374bd6
|
|
| MD5 |
65f85fc0c088ef54d8825cf9eba4601c
|
|
| BLAKE2b-256 |
d40c4f7c903788b2aea3467d37b9555902221730e92c9cb560856601c4bf92e0
|