3 ms·
As far as CPU instructions go, multiplication is _significantly_ faster (i.e. an order of magnitude) than exponentiation. That said, I can't speak to whether t
by chias 7y ago
As far as CPU instructions go, multiplication is _significantly_ faster (i.e. an order of magnitude) than exponentiation.
That said, I can't speak to whether the Rust compiler wouldn't just optimize that away -- it seems like unrolling exponentiation into multiplication for small constant powers would be a very safe and easy thing to do.
- raphlinus 7y agoIt certainly does do this: https://rust.godbolt.org/z/ZaDAKa https://rust.godbolt.org/z/ZaDAKa Not just small constant powers either. I tried .powi(1000000) and it compiled into a sequence of 25 vmulss instructions.
- chias 7y agoInteresting, thank you! I suspect then that defining it at x*x is just just because it's easier to type than x.powi(2).