parameter binding (?) is ~1.17× faster than string interpolation
Binding parameters is 17% faster because SQLite reuses the prepared statement — safety and speed point the same way here.
Result
| Run | parameter binding (?) | string interpolation | Ratio |
|---|---|---|---|
| Run 1 | 15.102 | 17.245 | 0.88× |
| Run 2 | 14.962 | 17.558 | 0.85× |
| Run 3 | 15.402 | 19.021 | 0.81× |
Spread over 3 runs: parameter binding (?) CV 1.2% / string interpolation CV 4.4% / Ratio CV 3.2%
The statement
con.execute('SELECT id FROM items WHERE id = ?', (i,)).fetchall() con.execute(f'SELECT id FROM items WHERE id = {i}').fetchall() Conditions
200 primary-key lookups.
Where this number does not apply
For a single query the prepare cost dominates and the gap disappears.
Reproduce
cd bench && python sqbench.py --samples 9 --out ../data/run.json