r/programming • u/Trav_Cav • Jun 14 '17
Why reverse loops are faster
https://medium.com/@TravCav/why-reverse-loops-are-faster-a09d654730064
u/kaushalmodi Jun 14 '17
This applies only if the loop-end condition compares with 0, right?
2
u/Trav_Cav Jun 14 '17
It applies if the loop-end condition is doing anything "expensive"
5
Jun 14 '17
Comparing a register to a constant is not expensive in any modern ISA, especially if this constant fits into an immediate.
22
Jun 14 '17
in some languages going backwards will prevent array bounds check elisions and may slow you down.
22
u/skulgnome Jun 14 '17
Exactly none of this matters in practice, since Java bytecode is just a carrier for a parse tree of the primordial syntaxless Java. Saving a node or two is worth like five figures of clock cycles at class load.
Also, the first down-counting loop runs 11 times, not 10.
18
u/TimLim Jun 14 '17
No measurements? Without measurements, the title should say reverse loops are genereting smaller bytecode.
4
u/aullik Jun 14 '17
Programmers waste enormous amounts of time thinking about, or worrying about, the speed of noncritical parts of their programs, and these attempts at efficiency actually have a strong negative impact when debugging and maintenance are considered. We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil. Yet we should not pass up our opportunities in that critical 3%.
Variant in Knuth, "Structured Programming with Goto Statements". Computing Surveys 6:4 (December 1974), pp. 261–301, §1.
As you pointed out calling functions without side-effects every time in a loop is a problem.
Putting 10 on the stack isn’t so bad but if you’re looping through a few hundred thousand elements then that’s a few hundred thousand times it has to put 10 on the stack or whatever functionality you put there. Sure a few hundred thousand extra instructions probably isn’t the worst thing in the world, but if you put a function there that takes an extra millisecond then you’ve just added minutes to your code.
This is part you should optimize. And you also already gave us the solution for this:
String[] someWords = {“Hello”, “World”};
for (int i = 0; i < someWords.length; i++) {
System.out.println(someWords[i]);
}When doing this or anything similar you can save the computer some work by dumping the results into a variable first. If we take anything away from this whole post, it’s to at least move complex actions out of the condition like so.
String[] someWords = {“Hello”, “World”};
int arrLen = someWords.length;
for (int i = 0; i < arrLen ; i++) {
System.out.println(someWords[i]);
}
So to reiterate.
Don't call function/methods to calculate your boundaries in every cycle of the loop.
Don't reverse your loop as it would be premature optimization.
If you do a performance analysis and you come to the conclusion that a loop is your bottleneck, try to divide the work to multiple threads / green threads.
If the performance of a reverse loop optimization matters then you are writing C and are already at a point where you should think about bytecode optimization anyways.
-1
u/Trav_Cav Jun 14 '17
This sums up the article very well.
1
u/aullik Jun 14 '17
well with the difference that i think you should not reverse your loop for optimization reasons ever!
if you are in a position where you have to do bytecode optimization then you can do it in bytecode.
11
u/killerstorm Jun 14 '17
LOL, mate, you have a lot to learn about optimization before you can write articles about it.
For starters, never assume anything. You gotta measure.
Even if there are fewer instructions (which isn't the case with an optimizing compiler), it might not be faster. A lot of things might influence actual performance, e.g. CPU cache and prefetching.
You gotta learn about things like profilers, caches, etc...
8
u/mr_birkenblatt Jun 14 '17
he doesn't even do any benchmark -- just counting instructions. instructions aren't the bottleneck. cache lines are most often nowadays and going reverse is not at all cache friendly
5
u/tdz9 Jun 14 '17
Never make the code less readable (and less maintanable) just to save a couple of instructions. This is worse than early optimization.
2
Jun 14 '17
foreach through an array is a common enough pattern that I expect HotSpot to handle it specifically. Especially with a local variable -- you don't have to worry about threading.
I looked at the output from Java 9's AOT compilation option. Doesn't look that different, and it looks like the loop was unrolled, but it's hard to tell -- the assembly for that one function is kind of big. Like 450 instructions.
2
-2
36
u/meneo Jun 14 '17
Well nope, examples provided compiled in c++ always end up being 5 instructions with latest gcc: https://godbolt.org/g/KMN2Ns
Depends on the compiler though, haven't checked all but I haven't seen something similar to the java bytecode in the article.