Compare performance against standard Python dict.
| 232 | |
| 233 | |
| 234 | class TestPerformanceComparison: |
| 235 | """Compare performance against standard Python dict.""" |
| 236 | |
| 237 | def test_insertion_comparable_to_dict(self): |
| 238 | """Test that insertion performance is comparable to dict.""" |
| 239 | size = 5000 |
| 240 | data = [(i, f"value_{i}") for i in range(size)] |
| 241 | |
| 242 | # Test dict |
| 243 | dict_obj = {} |
| 244 | with time_it() as dict_elapsed: |
| 245 | for key, value in data: |
| 246 | dict_obj[key] = value |
| 247 | |
| 248 | # Test B+ Tree |
| 249 | tree = BPlusTreeMap() |
| 250 | with time_it() as tree_elapsed: |
| 251 | for key, value in data: |
| 252 | tree[key] = value |
| 253 | |
| 254 | dict_time = dict_elapsed() |
| 255 | tree_time = tree_elapsed() |
| 256 | |
| 257 | # B+ Tree insertion can be slower than dict, but not by too much |
| 258 | # (dict has O(1) amortized, B+ Tree has O(log n)) |
| 259 | assert ( |
| 260 | tree_time < dict_time * 10 |
| 261 | ), f"B+ Tree insertion ({tree_time:.3f}s) is too slow compared to dict ({dict_time:.3f}s)" |
| 262 | |
| 263 | def test_ordered_iteration_faster_than_sorted_dict(self): |
| 264 | """Test that ordered iteration is faster than sorting dict items.""" |
| 265 | size = 10000 |
| 266 | data = [(random.randint(0, 100000), f"value_{i}") for i in range(size)] |
| 267 | |
| 268 | # Build dict |
| 269 | dict_obj = {} |
| 270 | for key, value in data: |
| 271 | dict_obj[key] = value |
| 272 | |
| 273 | # Build B+ Tree |
| 274 | tree = BPlusTreeMap() |
| 275 | for key, value in data: |
| 276 | tree[key] = value |
| 277 | |
| 278 | # Test sorted dict iteration |
| 279 | with time_it() as dict_elapsed: |
| 280 | sorted_items = sorted(dict_obj.items()) |
| 281 | |
| 282 | # Test B+ Tree iteration (already sorted) |
| 283 | with time_it() as tree_elapsed: |
| 284 | tree_items = list(tree.items()) |
| 285 | |
| 286 | dict_time = dict_elapsed() |
| 287 | tree_time = tree_elapsed() |
| 288 | |
| 289 | # B+ Tree iteration should be faster than sorting dict items |
| 290 | assert ( |
| 291 | tree_time < dict_time |
nothing calls this directly
no outgoing calls
no test coverage detected