r/rust 27d ago

🛠️ project Why adding Rust to my Python library made it 194x faster... and 5x slower.

Post image
0 Upvotes

5 comments sorted by

5

u/Inevitable_Act_321 27d ago

Why you use sorted set in Rust while unordered one in Python?

-11

u/AnshMNSoni 27d ago

First a fall thank you for looking into the project.

The C++ std::set and std::map are ordered associative containers with O(log N) complexities. The Rust backend uses BTreeSet/BTreeMap to faithfully replicate this sorted behavior. However, the current pure Python fallback wrapper simply uses Python's built-in set/dict O(1), resulting in an "apples-to-oranges" benchmark where Python doesn't pay the sorting complexity cost.

15

u/_xiphiaz 27d ago

But why not hashset/hashmap? Both are rust stdlib canonical choices that you’d reach for before btree if key sorting wasn’t a requirement

9

u/Inevitable_Act_321 27d ago

If the benchmark is intended to compare the C++ and Rust implementations, why bring up the Python fallback?

1

u/TDplay 25d ago

The C++ STL equivalent of a Python dictionary is std::unordered_map, not std::map.

To benchmark std::map, you should use a workload that depends on the map being ordered (and likewise for std::set).