commonquail/java-reverse-loop-benchmark


JMH benchmark of forward versus reverse loops

License: Other

Language: Java


JMH benchmark: reverse vs. forward loops

Do reverse loops run faster than forward loops in Java?

Conclusion

Stay tuned.

Premise

A blog post inspected the byte code of simple "forward" and "reverse" loops, found the latter to produce one less instruction, and concluded it must be faster. Unfortunately the post did not include a benchmark to support that conclusion.

A "forward" loop is a typical loop:

for (int i = 0; i < 42; i  ) {
    thing(i);
}

A "reverse" loop is the semantically equivalent version that traverses the opposite direction:

for (int i = 42; i >= 0; i--) {
    thing(i);
}

This transformation requires the operation to be associative, which certainly is no guarantee.

Benchmark

For each forward and reverse loop there is a benchmark that caches the loop termination condition ("Expression" in the specification) and one that doesn't, on the grounds that this saves another instruction. These are the cached respectively uncached benchmarks.

There are two sets of these four benchmarks, one that uses only the loop iterator and one that addresses the corresponding array element, on the grounds that that's why you'd have an array to begin with. The latter is the lookup benchmark set.

Appendix: build and run

  1. Install JDK 8 and Maven.
  2. Run make to build and execute the benchmark, writing the result to standard out as well as result.txt.

If you don't have make, build and run manually with

$ mvn package
$ java -jar target/benchmarks.jar

Project Statistics

Sourcerank 3
Repository Size 0 Bytes
Stars 1
Forks 0
Watchers 1
Open issues 0
Dependencies 0
Contributors 1
Tags 0
Created
Last updated
Last pushed

Top Contributors See all

Mikkel Kjeldsen

Something wrong with this page? Make a suggestion

Last synced: 2017-06-14 18:17:08 UTC

Login to resync this repository