1

I recently programmed a small application and there I needed to divide many, many numbers by 2. At first I just used the line i/2; but a friend of mine said i could just use the operator i>>=1 which produces the same numbers. Just for fun we meassured the time it took to compute and we found out that the i>>=1 method was on average 15% (30ms improvement) faster. (i is an int)

Why is that so? I just changed one operater nothing else.

Sir. Hedgehog
  • 1,260
  • 3
  • 17
  • 40
  • 2
    I'd say you're micro optimizing. You can trust JIT optimisations. – Shanu Gupta Oct 12 '18 at 11:18
  • On machine code level: bit shift is a relatively simple operation; multiplication is done more or less as you learned at school, for every bit 1 the other parameter shifted and added to a result. – Joop Eggen Oct 12 '18 at 11:28

0 Answers0