r/Python • • 1d ago

News I Built A C++ Powered Python Library Using pybind11 - way faster than python loops

Hey everyone, I just published my first C++ powered Python library on PyPI (kaptandanuj-easyhelper) to see how much faster list operations get when you move the loop from Python to C++.

Right now it has an order-preserving unique() function for int, float, and str lists. Under the hood it uses a templated C++ std::vector + std::unordered_set with reserve() pre-allocation, bound with pybind11.

On a list of 100k items, it beats a normal Python for loop + set()

Here how you install it

pip install kaptandanuj-easyhelper

0 Upvotes

5 comments sorted by

5

u/PLPeeters 1d ago

Not to burst your bubble, but did you benchmark it against list(dict.fromkeys(items)) instead of an explicit Python loop?

dict.fromkeys() is already implemented in C in CPython, preserves insertion order, and avoids the Python-to-C++ type conversion overhead introduced by pybind11.

6

u/AdMammoth8075 1d ago

Fair point!! It is significantly slower than dict.fromKeys() and my aim is not to make fastest library i just want to know the architecture behind making this type of libraries and it's my first step

1

u/Distinct-County-680 1d ago

Congrats you are discovered that Python is sometimes just the expensive middleman.

1

u/remic_0726 1d ago

Pas toujours, trop souvent le problème se situe entre la chaise et le clavier. Et utiliser le profiler de python peut faire des miracles, même avec des anciennes versions moins véloces. D'ailleurs cela est aussi valable en micropython, une bonne optimisation des quelques lignes te fait gagner de nombreuses heures fastidieuses de codage en C, et encore le C il faut savoir l'apprivoiser, un débordement de tampon ou une fuite mémoire et la belle optimisation se transforme en cauchemar pour le néophite.