A powerful Python Time and Space Complexity Analyzer that helps you understand the computational complexity of your code. TimeSpaceX analyzes your Python functions and provides detailed explanations of their time and space complexity using Big O notation.
- 📊 Analyzes both time and space complexity
- 📝 Provides detailed explanations for each analysis
- 🎯 Detects common algorithmic patterns:
- Simple loops
O(n)
- Nested loops
O(n²)
,O(n³)
- Binary search patterns
O(log n)
- Divide and conquer algorithms
O(n log n)
- Recursive functions
- Matrix operations
- Simple loops
- 🎨 Beautiful command-line interface with syntax highlighting
- 🔍 Smart pattern recognition for common algorithms
- 💡 Educational tool for learning algorithmic complexity
pip install timespacex
Analyze a Python file:
timespacex your_file.py
Options:
timespacex --no-color your_file.py # Disable colored output
def sum_elements(arr):
total = 0
for element in arr:
total += element
return total
Output:
╭─ Function: sum_elements ──────────────────────╮
│ The function `sum_elements` has a time │
│ complexity of O(n). This is because the │
│ function iterates through the input exactly │
│ once. │
│ │
│ The space complexity is O(1). This is because│
│ the function uses a constant amount of extra │
│ space regardless of input size. │
╰──────────────────────────────────────────────╯
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
Output:
╭─ Function: binary_search ──────────────────────╮
│ The function `binary_search` has a time │
│ complexity of O(log n). This is because the │
│ function uses a binary search pattern, │
│ dividing the search space in half at each │
│ step. │
│ │
│ The space complexity is O(1). This is because │
│ the function uses a constant amount of extra │
│ space regardless of input size. │
╰───────────────────────────────────────────────╯
def matrix_multiplication(matrix1, matrix2):
result = [[0 for _ in range(len(matrix2[0]))]
for _ in range(len(matrix1))]
for i in range(len(matrix1)):
for j in range(len(matrix2[0])):
for k in range(len(matrix2)):
result[i][j] += matrix1[i][k] * matrix2[k][j]
return result
Output:
╭─ Function: matrix_multiplication ─────────────╮
│ The function `matrix_multiplication` has a │
│ time complexity of O(n³). This is because │
│ the function performs matrix multiplication │
│ with three nested loops. │
│ │
│ The space complexity is O(n²). This is │
│ because the function creates a new matrix │
│ that grows quadratically with input size. │
╰─────────────────────────────────────────────╯
TimeSpaceX can detect and analyze:
-
⏱ Time Complexity Patterns:
- Linear iterations
O(n)
- Nested loops
O(n²)
,O(n³)
- Binary search
O(log n)
- Divide and conquer
O(n log n)
- Recursive patterns
- Matrix operations
- Exponential complexity
O(2^n)
- Linear iterations
-
💾 Space Complexity Patterns:
- Constant space
O(1)
- Linear space
O(n)
- Quadratic space
O(n²)
- Recursive stack space
- Dynamic array growth
- Matrix space requirements
- Constant space
Contributions are welcome! Here's how you can help:
- Fork the repository
- Create a new branch (
git checkout -b feature/amazing-feature
) - Make your changes
- Commit your changes (
git commit -m 'Add amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- Inspired by bigocalc.com
- Built with rich for beautiful terminal output
For questions, suggestions, or contributions, please use GitHub Issues.
Project Link: https://github.com/hamedyaghoobian/timespacex