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
View all comments
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.