Originally posted by Jay Damon:
When coding for loops, I often code something similar to the following example:
to create a local variable containing the size of the array.
Originally posted by Layne Lund:
I also agree with Ilja that creating a new variable to store the array length is a questionable "optimization". In my opinion, it makes the code much more difficult to read, which is as much of a concern, if not more, than performance.
I recall similar kinds of optimization discoveries being completely invalidated by the next rev of the machine's underlying microcode, resulting in large swathes of code now being (marginally) slower and harder to read.
Originally posted by JuanP barbancho:
try to use byte or short instead of int.
I think that is more quick.
You'll get no argument from me regarding the actual impact of any of these. Too often I've found that the strongest proponents of these pico-optimisations are the most likely to write code that parses large XML documents multiple times, or recovers static data from distant servers with multiple requests per session. Opening one file unnecessarily will likely wipe out any gain to be had from this level of analysis.
Note that optimizing at this level is really, really, REALLY pointless. HotSpot does strange and wonderful things to your code, so you have no idea which is "better" at runtime. Worse (and even more likely), one bad algorithm involving I/O will suck up more time than that squeezed out of painstakingly optimizing a bazillion for-loops.
Optimize your design, and write your loops in a way that makes them most readable...
Without deviation from the norm, progress is not possible - Zappa. Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koophttps://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton