Re:Faster CPython at PyCon, part one
by vbarrielle on Thursday May 11, 2023 @04:53PM
(#35908233)
Due to the possibility to fuse multiple operations in C++ (whereas you often have intermediate arrays in numpy), I routinely get 20x speedups when porting from numpy to C++. Good libraries like eigen help a lot.
[ Parent
| on HN ]
What's an example of fusing operations?
Are you talking about combinations of operations that are used commonly enough to warrant Eigen methods that perform them at once in SIMD?
Probably that eigen uses expression templates to avoid the needles creation of temporaries.
most non-trivial numpy operations require temporaries that require new allocations and copies. Eigen3's design lets you avoid these through clever compilation tricks while remaining high-level.
sometimes numpy can elide those (e.g. why a+=b is faster than a=a+b) but this it not possible in general. Sometimes people use monstrosities like einsum... but I find it more intuitive to just write in C or C++...
In addition to the time spent in allocation / gc / needless copying, the memory footprint can be higher by a factor of a few (or more...).
Yep, einsum is included in "doing numpy right." And for what it's worth, it's horrid to use and still won't get around cases like x -> cos(x). I haven't needed the power of eigen for a couple of years, but I appreciate the tip.