Pages

May 28, 2015

[Python] Don't use "<<" to double a number

As we know, we can use "a << b" to represent "a*(2^b)." But this method is much slower than just using addition, performing "a+=a" for b times. In other words, "<<" operation is much more expensive than "+" operation.

I have performed some simple experiments. The results are shown below.


As we can see, the running time for "<<" operations are much slower than that of "+" operation. It takes more than several hundred times for "<<" to produce the same result as "+" does. Therefore, using "+" operation instead of using "<<" for the same purpose. (Here I am under the restriction of not using multiplication.)

No comments:

Post a Comment