Show early termination advantages.
()
| 244 | |
| 245 | |
| 246 | def demonstrate_early_termination(): |
| 247 | """Show early termination advantages.""" |
| 248 | print("=== Early Termination Advantage ===\n") |
| 249 | |
| 250 | size = 50000 |
| 251 | data = create_test_data(size) |
| 252 | |
| 253 | bplustree = BPlusTreeMap(capacity=128) |
| 254 | bplustree.update(data) |
| 255 | |
| 256 | regular_dict = dict(data) |
| 257 | |
| 258 | # Find first 10 items where key > 40000 |
| 259 | print("Find first 10 items where key > 40,000:") |
| 260 | |
| 261 | # B+ Tree approach |
| 262 | def bplus_early_termination(): |
| 263 | result = [] |
| 264 | for key, value in bplustree.range(40000, None): |
| 265 | result.append((key, value)) |
| 266 | if len(result) >= 10: |
| 267 | break |
| 268 | return result |
| 269 | |
| 270 | bplus_time, bplus_result = benchmark_function(bplus_early_termination) |
| 271 | print(f" B+ Tree: {bplus_time*1000:.3f} ms (found {len(bplus_result)} items)") |
| 272 | |
| 273 | # Dict approach (must scan and sort) |
| 274 | def dict_early_termination(): |
| 275 | result = [] |
| 276 | for key, value in sorted(regular_dict.items()): |
| 277 | if key >= 40000: |
| 278 | result.append((key, value)) |
| 279 | if len(result) >= 10: |
| 280 | break |
| 281 | return result |
| 282 | |
| 283 | dict_time, dict_result = benchmark_function(dict_early_termination) |
| 284 | print(f" Dict: {dict_time*1000:.3f} ms (found {len(dict_result)} items)") |
| 285 | |
| 286 | if dict_time > 0: |
| 287 | speedup = dict_time / bplus_time |
| 288 | print(f" → B+ Tree is {speedup:.1f}x faster for early termination queries!") |
| 289 | |
| 290 | |
| 291 | def capacity_tuning_demo(): |
no test coverage detected