Dividing by 2 fast: how?
Please consider the following example:
tt=1200
ss=tt>>2
print ss
300
The division by 4 goes fast. But how can I divide by 2?
The expression tt>2 yields a boolean. And the options ss=tt//2 and ss=tt; ss/=2 seem not optimal.
timeit("tt=1200;ss=tt>>2") 625 loops, best of 3: 152 ns per loop
timeit("tt=1200;ss=tt//2") 625 loops, best of 3: 323 ns per loop
timeit("tt=1200;ss=tt;ss/=2") 625 loops, best of 3: 552 ns per loop
Thanks for you kind consideration.
Is this homework? Do you understand what the bit-shift operator does? If not, look up http://docs.python.org/2/library/stdtypes.html#bitwise-operations-on-integer-types