SQL BenchMEASURED, NOT GUESSED

    ↑↓ to select, Enter to open, Esc to close

    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

    Runparameter binding (?)string interpolationRatio
    Run 115.10217.2450.88×
    Run 214.96217.5580.85×
    Run 315.40219.0210.81×

    Spread over 3 runs: parameter binding (?) CV 1.2% / string interpolation CV 4.4% / Ratio CV 3.2%

    parameter binding (?): 15.10msstring interpolation: 17.25msRun1parameter binding (?): 14.96msstring interpolation: 17.56msRun2parameter binding (?): 15.40msstring interpolation: 19.02msRun3

    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