A comprehensive resource documenting the time and space complexity of Python's built-in functions and standard library operations across different Python versions and implementations.
Overview
This project provides detailed documentation of algorithmic complexity for:
- Python Built-ins:
list,dict,set,str, etc. - Standard Library Modules:
collections,heapq,bisect,annotationlib,compression.zstd, and more - Python Versions: 3.10ā3.14 (including new 3.14 features)
- Alternative Implementations: CPython, PyPy, Jython, IronPython
Features
- š Comprehensive complexity tables for all major built-in types and operations
- š Version-specific behavior and optimization changes
- š Implementation-specific notes (CPython vs PyPy vs others)
- š ļø CLI Tool for estimating complexity of your own code
- š Interactive search and filtering
- š± Mobile-friendly responsive design
Website
Visit the documentation at: pythoncomplexity.com
Quick Start
Prerequisites
- Python 3.10+ (3.14 recommended)
- uv - Fast Python package manager
- Git
Installation
# Install uv (one-time) curl -LsSf https://astral.sh/uv/install.sh | sh # Clone and set up git clone https://github.com/heikkitoivonen/python-time-space-complexity.git cd python-time-space-complexity # Install dependencies uv sync # Start development server make serve # Open http://localhost:8000
Development Commands
Using Make (Recommended)
make help # See all available commands make dev # Install dev environment make serve # Serve documentation locally make build # Build static site make check # Run lint + types + tests make lint # Run linter make format # Format code make types # Run type checker make test # Run tests make clean # Clean build artifacts make update # Update dependencies
Using uv Directly
uv sync # Sync dependencies uv run mkdocs serve # Run command in venv uv add package-name # Add dependency uv add --dev pytest-plugin # Add dev dependency uv lock --upgrade # Update dependencies
Complexity Estimator CLI
Measure the Big-O complexity of your own Python functions:
# Usage: python scripts/estimate_complexity.py <module> <function>
python scripts/estimate_complexity.py my_script my_functionExample output:
Input Size (n) | Avg Time (s)
-----------------------------------
100 | 0.000003
500 | 0.000012
...
Estimated Complexity: O(n) (Linear)
Project Structure
āāā docs/ # MkDocs documentation source
ā āāā index.md # Landing page
ā āāā builtins/ # Built-in types (list, dict, set, tuple, str)
ā āāā stdlib/ # Standard library modules
ā āāā implementations/ # CPython, PyPy, Jython, IronPython
ā āāā versions/ # Python version guides (3.10ā3.14)
āāā data/ # JSON data files
āāā scripts/ # Utility scripts
āāā tests/ # Test files
āāā .github/workflows/ # GitHub Actions CI/CD
ā āāā deploy.yml
āāā pyproject.toml # Project metadata and dependencies
āāā mkdocs.yml # MkDocs configuration
āāā Makefile # Development commands
Development Workflow
1. Create Feature Branch
git checkout -b feature/add-numpy-complexity
2. Make Changes & Test Locally
vim docs/new-module.md
make serve # View at http://localhost:80003. Run Quality Checks
make lint # Check code quality make format # Auto-format code make types # Type checking make test # Run tests make check # All checks (required before commit)
4. Commit & Push
git add . git commit -m "Add: NumPy array complexity documentation" git push origin feature/add-numpy-complexity
Adding Documentation
- Create markdown file in
docs/ - Add link to
mkdocs.ymlnavigation - Test locally with
make serve - Run
make checkbefore committing
Code Quality Standards
Linting & Formatting
- ruff for linting (line length: 100 chars, Python 3.10+ compatibility)
- pyright for static type checking
- pytest for testing
Commit Messages
Type: Brief description
Types: Add, Fix, Update, Refactor, Docs, Test, Chore
Example: Add: List complexity documentation
Quick Reference - Python Complexity Cheat Sheet
Lists
| Operation | Time | Notes |
|---|---|---|
append() |
O(1)* | Amortized |
insert(i) |
O(n) | Shifts elements |
pop() |
O(1) | Last element |
pop(0) |
O(n) | First element |
in |
O(n) | Linear search |
sort() |
O(n log n) | Timsort/Powersort |
Pro tip: Use deque.appendleft() for O(1) prepend instead of list.insert(0).
Dictionaries & Sets
| Operation | Time |
|---|---|
d[key] |
O(1) avg |
d[key] = v |
O(1) avg |
key in d |
O(1) avg |
set.add() |
O(1) avg |
x in set |
O(1) avg |
Pro tip: Use sets for fast membership testing, not lists.
Strings
| Operation | Time |
|---|---|
len() |
O(1) |
s[i] |
O(1) |
in (substring) |
O(n) avg |
split() / join() |
O(n) |
Pro tip: Use "".join(list) not += in loops.
Standard Library
| Module | Operation | Time |
|---|---|---|
| deque | append() / appendleft() |
O(1) |
| deque | pop() / popleft() |
O(1) |
| heapq | heapify() |
O(n) |
| heapq | heappush() / heappop() |
O(log n) |
| bisect | bisect_left/right() |
O(log n) |
Common Patterns
# ā Bad: O(n) membership check if item in list: pass # ā Good: O(1) membership check if item in set: pass # ā Bad: O(n²) string concatenation result = "" for item in items: result += item # ā Good: O(n) string building result = "".join(items) # ā Bad: O(n) prepend lst.insert(0, item) # ā Good: O(1) prepend from collections import deque dq = deque() dq.appendleft(item)
Python Version Performance
Python 3.10 ā Baseline
Python 3.11 ā +10-60% improvements (inline caching!)
Python 3.12 ā +5-10% improvements
Python 3.13 ā Similar (experimental free-threading)
Python 3.14 ā Better GC pauses, new heapq max-heap
Implementation Comparison
| Implementation | Use Case | Speed | GIL |
|---|---|---|---|
| CPython | Default, standard | Good | Yes |
| PyPy | CPU-bound loops | Excellent* | No |
| Jython | Java integration | Good | No |
| IronPython | .NET integration | Good | No |
Deployment
GitHub Pages Setup
- Push to GitHub
- Go to Settings ā Pages
- Select Deploy from a branch ā gh-pages
- GitHub Actions automatically deploys on push
Custom Domain (Optional)
- Update
site_urlinmkdocs.yml - Configure DNS to point to GitHub Pages
- In GitHub Settings ā Pages, enter custom domain
- Enable HTTPS
Troubleshooting
Build Issues
make clean && make build
uv run mkdocs serve --verboseDependency Issues
rm -rf .venv/ && uv syncGitHub Pages Not Updating
- Check GitHub Actions tab for errors
- Verify gh-pages branch exists
- Wait ~1-2 minutes for deployment
Sources & References
- Python Official Documentation
- TimeComplexity Wiki
- Python Enhancement Proposals (PEPs)
- uv Documentation
- MkDocs Documentation
- Material for MkDocs
Contributing
Contributions are welcome! Please see CONTRIBUTING.md for guidelines.
License
MIT License - See LICENSE.txt for details
Disclaimer
While we strive for accuracy, complexity information may vary based on specific implementations and versions. Always verify with official documentation and benchmarks for performance-critical code.