[java] bit shifting
The compiler doesn''t. Then again, the only optimization I''ve seen a java compiler make (well, since they disabled the -O option, anyway) is evaluating constant expressions like 5 + 3 at compile time.
However, whether the compiler optimizes or not has largely become an irrelevent question in java programming. JVMs that perform JIT compilation, which is just about all of them these days, make some incredible optimizations at runtime. Almost all error checking that is provably unnecessary is eliminated, and tail recursion is converted to iteration instead. Given that those are common optimizations done at runtime, I''m willing to bet that things like converting division to bitshifts is also a common runtime optimization.
However, whether the compiler optimizes or not has largely become an irrelevent question in java programming. JVMs that perform JIT compilation, which is just about all of them these days, make some incredible optimizations at runtime. Almost all error checking that is provably unnecessary is eliminated, and tail recursion is converted to iteration instead. Given that those are common optimizations done at runtime, I''m willing to bet that things like converting division to bitshifts is also a common runtime optimization.
Upon benchmarking, I''ve found that the JIT doesn''t make that optimization either. I also found out why.
(-5 / 2) == -2
(-5 >> 1) == -3
So, with java anyway, >> 1 is not equivalent to / 2 in the general case, meaning that substitution would be an incorrect optimization. And as we should all know, an incorrect optimization is NOT an optimization at all.
(-5 / 2) == -2
(-5 >> 1) == -3
So, with java anyway, >> 1 is not equivalent to / 2 in the general case, meaning that substitution would be an incorrect optimization. And as we should all know, an incorrect optimization is NOT an optimization at all.
-5 >>> 1 == 2147483645
Yeah, don''t think that works...
"So crucify the ego, before it''s far too late. To leave behind this place so negative and blind and cynical, and you will come to find that we are all one mind. Capable of all that''s imagined and all conceivable."
- Tool
------------------------------
Yeah, don''t think that works...
"So crucify the ego, before it''s far too late. To leave behind this place so negative and blind and cynical, and you will come to find that we are all one mind. Capable of all that''s imagined and all conceivable."
- Tool
------------------------------
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement