Commit 0503cc7
authored
⚡️ Speed up method
Here's a rewritten, optimized version of your program, focusing on what the line profile indicates are bottlenecks.
- **Reuse cursor**: Opening a new cursor repeatedly is slow. Maintain a persistent cursor.
- **Batching commits**: Commit after many inserts if possible. However, since you clear the buffer after each write, one commit per call is necessary.
- **Pragma optimizations**: Set SQLite pragmas (`synchronous = OFF`, `journal_mode = MEMORY`) for faster inserts if durability isn't paramount.
- **Avoid excessive object recreation**: Only connect if needed, and clear but *do not reallocate* the benchmark list.
- **Reduce exception handling cost**: Trap and re-raise only actual DB exceptions.
**Note:** For highest speed, `executemany` and single-transaction-batch inserts are already optimal for SQLite. If even faster, use `bulk insert` with `INSERT INTO ... VALUES (...), (...), ...`, but this requires constructing SQL dynamically.
Here’s the optimized version.
**Key points:**
- `self._ensure_connection()` ensures both persistent connection and cursor.
- Pragmas are set only once for connection.
- Use `self.benchmark_timings.clear()` to avoid list reallocation.
- The cursor is reused for the lifetime of the object.
**If your stability requirements are stricter** (durability required), remove or tune the PRAGMA statements. If you want even higher throughput and can collect many queries per transaction, consider accepting a "bulk flush" mode to reduce commit frequency, but this requires API change.
This code preserves your public API and all comments, while running considerably faster especially on large inserts.CodeFlashBenchmarkPlugin.write_benchmark_timings by 111% in PR #217 (proper-cleanup)1 parent 62e10b1 commit 0503cc7
1 file changed
+15
-5
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
| 20 | + | |
20 | 21 | | |
21 | 22 | | |
22 | 23 | | |
| |||
47 | 48 | | |
48 | 49 | | |
49 | 50 | | |
50 | | - | |
51 | | - | |
| 51 | + | |
52 | 52 | | |
53 | 53 | | |
54 | | - | |
55 | 54 | | |
56 | | - | |
| 55 | + | |
57 | 56 | | |
58 | 57 | | |
59 | 58 | | |
60 | 59 | | |
61 | | - | |
| 60 | + | |
62 | 61 | | |
63 | 62 | | |
64 | 63 | | |
| |||
290 | 289 | | |
291 | 290 | | |
292 | 291 | | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
293 | 303 | | |
294 | 304 | | |
0 commit comments