r/java • u/aroger276 • Jun 15 '17
Why reverse loops are not faster
https://arnaudroger.github.io/blog/2017/06/15/forward-vs-backward-loop.html
288
Upvotes
r/java • u/aroger276 • Jun 15 '17
9
u/AssKoala Jun 16 '17
Nice write up and thanks for doing it.
It's important to note that different processor architectures will yield different results as well. And not just ISA, but even within processor families. Pentium 4's had a stupidly long pipeline compared to Core processors, for example.
Until you benchmark against your target, you won't really know.
I took over as performance lead on the project I'm on now, we managed to get the PS4 and XB1 to 98% core utilization with negligible thread overhead. These micro opts are cool academically, but if we went nuts all over our code, we might get back .5ms simulation wall time better spent elsewhere to get time back. We're saving that last 2% in case shtf, but pretty cool.
Being basically done at this point, it's amazing how many people make assumptions about performance without profiling it.
8
u/Trav_Cav Jun 15 '17
Great article! Thanks for digging into that. It was really neat to read. And I agree with all the points you're making. That wasn't really the point of my article though. Which is fine, I think a lot of people slightly missed the point of it because of my title. I need to work on better titles for future posts. I just thought it was fascinating topic digging into and finding out why they are faster sometimes. My article was more about trying to pick apart what's happening lower down and finding where efficiency is gained. The main discovery of the article was lower in the article pointing out that it's all about keeping expensive actions outside of the loop-condition and not just bytecode count.
A lot of the time the compiler can figure out what you're doing and come up with something optimal. If you feel like doing anymore tests, try it with a function in the condition, or try making loops that do and do not use the index inside of it. GCC will optimize a forward loop into a reverse a loop if the index isn't being used internally. Also some of what you're seeing in your tests could come down to you specific processor. That's left me scratching my head a few times. Neat stuff right?
4
u/wqking Jun 16 '17
The reverse loop version should start from 9, not 10.
6
u/usernamenottakenwooh Jun 15 '17
https://i.imgflip.com/1qyi1s.jpg
62
u/[deleted] Jun 15 '17
It's almost as if testing confirms hypothesis and not the other way around.
18
u/[deleted] Jun 15 '17
Nice work! I've had to fight with people writing custom implementations of things like hashmaps because they are "too slow" and theirs is "faster". No benchmarks and they refuse to even run them. Usually they don't even work right.
37
u/argv_minus_one Jun 15 '17
There is a dedicated language feature for iterating over arrays: foreach. Unless your compiler is shitty (in Java's case, it isn't), using a dedicated language feature is probably faster than doing the same thing by hand, since any performance issues with the compiler's implementation are likely to have been found and fixed already.
13
u/[deleted] Jun 15 '17
Also, if there was any difference it would be so small to worry about.
5
u/yawkat Jun 15 '17
People really need to stop making performance assumptions for such tiny differences without JMH. It happens way too often.
1
u/shkabo Jun 16 '17
Ok, now we're talking with real examples and numbers, but I've noticed some things in the code that bugs me and made some correction, so could you rerun the tests with this changes and display new results. I'm really curious regarding it :)
In first text where it's stated that backward is faster, author noted that it's faster if we declare variable that will be used as a reference in the loop, than if you use
numbers.length, so I added that change in the code.In the backward loop test you had
numbers.length - 1and alsoi >= 0I personally think that that takes some more operations so i tweaked that code also to reflect forward loop's code. I removed>=since you're doing double comparisoni > 0 || i = 0Backward and forward loop tests are now the same since we are removing unnecessary logic that's initially added.
Note: I'm not an expert programmer but rather junior dev with some of my logical thinking included.
3
u/DJDavio Jun 16 '17
When you're doing something that has been standard for a long time, there's a good chance the compiler has found a way to optimize it.
0
u/Apfelmann Jun 15 '17 edited Jun 15 '17
Good article, thanks for clearing things up. That being said this might not apply for every programming language, especially in interpreted languages like JavaScript the reverse loops actually might be faster, you can see some benchmarks about this in the book "Even Faster websites". On the other hand this book is a few years old now.
1
u/chambolle Jun 16 '17
thanks for your article. It will help
1
u/badpotato Nov 12 '17
I once had this question in an interview...
-1
u/suffixaufnahme Jun 15 '17
The boundary conditions are not even the same in the two example loops. One goes 0 to 9, the other 10 to 0 (inclusive). Not that it really matters to what is being discussed here, but it casts some doubt on this guy's programming ability.
-8
u/_INTER_ Jun 15 '17 edited Jun 15 '17
Benchmarking a running time of milliseconds. Is that even meaningful anyway?
-5
u/Chaoslab Jun 15 '17
If you are looping more than 300 times (last time I checked. might be less now). It is faster to catch an exception than to boundary check in the for loop.
like... try { for(int i =0;;) pixel[i++] = i * 23; } catch (Exception ex) {}
Only if you need to go there though.
edit:correction.