Python Numbers Every Programmer Should Know
4 months ago
- #benchmark
- #python
- #performance
- Python performance benchmarks covering memory usage, basic operations, collections, JSON serialization, web frameworks, file I/O, database operations, and async overhead.
- Memory costs: Empty Python process uses 15.73 MB, empty string 41 bytes, small int 28 bytes, empty list 56 bytes, empty dict 64 bytes.
- Basic operations: Adding integers takes 19.0 ns, list append 28.7 ns, list comprehension (1,000 items) 9.45 μs.
- Collections: Dict lookup by key 21.9 ns, set membership check 19.0 ns, list membership check (1,000 items) 3.85 μs.
- JSON serialization: orjson.dumps() (complex) 310 ns (3.2M ops/sec), stdlib json.dumps() 2.65 μs (376.8k ops/sec).
- Web frameworks: FastAPI (return JSON) 8.63 μs (115.9k req/sec), Django 18.1 μs (55.4k req/sec).
- File I/O: Open and close file 9.05 μs, read 1KB file 10.0 μs, write 1MB file 207 μs.
- Database: SQLite insert (JSON blob) 192 μs (5.2k ops/sec), MongoDB find_one by _id 121 μs (8.2k ops/sec).
- Async overhead: run_until_complete(empty) 27.6 μs (36.2k ops/sec), asyncio.sleep(0) 39.4 μs (25.4k ops/sec).
- Key takeaways: Python objects have significant memory overhead, dict/set lookups are extremely fast, alternative JSON libraries are 3-11x faster than stdlib json, async has measurable overhead.