I know power of 2 can be implemented using << operator.
What about power of 10? Like 10^5? Is there any way faster than pow(10,5) in C++? It is a pretty straight-forward computation by hand. But seems not easy for computers due to binary representation of the numbers… Let us assume I am only interested in integer powers, 10^n, where n is an integer.
C++ – Faster Alternatives to pow() for Integer Power of 10
c++numerical
Best Answer
Something like this:
Obviously, can do the same thing for
long long
.This should be several times faster than any competing method. However, it is quite limited if you have lots of bases (although the number of values goes down quite dramatically with larger bases), so if there isn't a huge number of combinations, it's still doable.
As a comparison:
Compiled with g++ 4.6.3, using
-Wall -O2 -std=c++0x
, gives the following results:(I did have an option for using
pow
as well, but it took 1m22.56s when I first tried it, so I removed it when I decided to have optimised loop variant)