Discover performance impact of different Java libraries.
Three approaches are tested:
- Using
org.apache.commons.lang3.reflectFieldUtils and MethodUtils. This is the slowest approach. - Using classic Java reflection
ClassgetField and getMethod. About 4 to 5 times as fast as using apache. - Using the
java.lang.invokeVarHandle and MethodHandle (added in Java 1.7). Similar to classic.
Observe that when using classic reflection or invoke package, the major performance gain is achieved because we can get a reference to the method or the field, cash it, then reuse it during the repeated tests.
Using com.fasterxml.jackson.core:jackson-databind library to deserialize a JSON text to
a simple object graph.
Two approaches are tested:
- Using
ObjectMapper. Simple to implement. About 27 lines of code. - Using
JsonParser. Harder to implement. About 150 lines of code. About 10 times faster than usingObjectMapper.
Using eclipse-collections:MutableIntList for a list on integers compared with JDK List class.
The MutableIntList is about 4 times faster than the JDK's List.
Using several approaches to generate random numbers, next is a list ordered by performance, fatser is first:
java.util.concurrent.ThreadLocalRandom: this was the fastest in generating random numbersjava.util.Random: about 5 times slower than ThreadLocalRandomStrictMath.random(): about 6 times slower than ThreadLocalRandomMath.random(): about 10 times slower than ThreadLocalRandomSecureRandom.getInstanceStrong(): about 100 times slower than ThreadLocalRandomnew SecureRandom(): about 110 times slower than ThreadLocalRandom
- If you do not need a secure random, use ThreadLocalRandom.
- If you do need a secure random, use SecureRandom.getInstanceStrong()
- On some platforms, SecureRandom has the potential of blocking for long time. Be careful.
Using several approaches:
- JDK standard MessageDigest.
- Apache commons codec.
- Guava.
- SHA3_256 JDK.
- SHA3_256 Apache commons Codec.
- SHA3_256 Keccak. About 2 to 3 times slower than others.
The first 5 approaches perform relatively at the same scale, but SHA3 is slower than SHA2.
If you do not need a secure hash, then there are faster and simpler
algorithms. Consider using org.apache.commons.codec.digest.XXHash32.
When the string to find/replace is simple, consider using org.apache.commons.lang3.StringUtils.replace()
instead of String.replaceAll(), because replaceAll() takes a RegEx.
The StringUtils.replace() is about 50% faster than String.replaceAll().
Creating an instance of HttpClient is expensive, reuse the instance if you can.
In an example where an HTTP GET (to same URL) is called 1000 times, reusing the instance
was 3 to 4 times faster and consumed about 1/3 of the memory.
Instances of HttpClient are not quickly garbage collected, so creating a lot of instances could
crash the JVM.
Math.fma(a, b, c) returns a * b + c but it does only one rounding instead of two, thus
fma is more accurate.
- Java 17: The
fmamethod is about 15% faster than hand-coding the calculation. - Java 21: The
fmamethod is as fast as the hand-coding the calculation;