json.dumps is ~2.62× slower than pickle.dumps
Serialising the same small dict 20,000 times: pickle is ~2.6× faster than json.
Result
| Run | json.dumps | pickle.dumps | Ratio |
|---|---|---|---|
| Run 1 | 56.742 | 22.791 | 2.49× |
| Run 2 | 56.495 | 21.661 | 2.61× |
| Run 3 | 58.300 | 21.637 | 2.69× |
Spread over 3 runs: json.dumps CV 1.4% / pickle.dumps CV 2.5% / Ratio CV 3.2%
Conditions
A 4-key dict dumped 20,000 times; pickle protocol 5.
Where this number does not apply
pickle wins on speed only; json wins on portability, and unpickling untrusted data is unsafe.
Reproduce
cd bench && python micro_py.py --out ../data/run.json --samples 21