Jafama is a Java library that provides fast numeric operations that are replacements for the methods on java.lang.Math. The description on its web site is the following:
Jafama (Java Fast Math) consists of fast - but not sloppy - counterparts of java.lang.Math treatments, plus additional ones. They are usually about 2-4 (up to 15) times faster, with about 1e-15 accuracy, and handle special cases (NaN, etc.).
It’s a pretty easy drop in replacement. You would write
FastMath.sin(angle) instead of
Math.sin(angle) in Java. 2 to 4 times faster with a very small accuracy loss sounds like it could be too good to be true, especially for what seems like a semi-obscure Java library hosted on Sourceforge with no presence in public Maven repos. Also, Jafama offers many math functions. Are they all equally fast? Are the accuracy claims realistic?
I’ll show how we can use Clojure to verify the libraries claims of accuracy and performance. The code for this blog post is located in https://github.com/jasongilman/jafama-perf-test
FastMath vs StrictFastMath
The JVM allows for different implementations of Math operations to use platform specific implementations of the underlying mathematical functions like
sin. This use of the underlying microprocessor instructions can give much better performance but can lead to slightly different results when running the same Java code on different hardware platforms. Most of the time slight differences are an acceptable trade off for faster run times. Occasionally you’ll want your software to have the exact same result regardless of hardware. Java provides the StrictMath class for that purpose. It’s slower but is guaranteed to have the same answer regardless of hardware.
Jafama has two main classes FastMath and StrictFastMath that are akin to java.lang.Math and java.lang.StrictMath. Use FastMath when you want the best performance and slightly different results on different hardware is okay. Use StrictFastMath when the same answers on any hardware is important to your use cases. If you’re interested in a library like Jafama, which offers better performance at the cost of some accuracy, then you’re probably okay with FastMath which will offer slightly better performance. The tests in this blog post will focus on testing with FastMath only.
We’ll measure performance comparing java.lang.Math and the Jafama FastMath for most of the functions available on the class. We’ll use the Clojure Criterium library to measure performance. Criterium is a great tool for benchmarking from Clojure. It automatically includes “a warm-up period, designed to allow the JIT compiler to optimise its code” and takes factors like garbage collection into account. It runs the code being benchmarked many times over in order to provide a realistic estimate of performance.
We can use Criterium to measure the performance of calling
sin on the Jafama FastMath class with the following code:
1 2 3 4 5 6 7 (use 'criterium.core) (import 'net.jafama.FastMath) (let [v 5.0] (with-progress-reporting (bench (FastMath/sin v))))
That takes between 1 and 2 minutes to run. When it’s done you get some output that looks like the following. (Not the actual results for sin.)
1 2 3 4 5 6 7 8 9 10 11 Evaluation count : 1094186460 in 60 samples of 18236441 calls. Execution time mean : 51.726800 ns Execution time std-deviation : 3.907849 ns Execution time lower quantile : 47.776146 ns ( 2.5%) Execution time upper quantile : 62.547784 ns (97.5%) Overhead used : 6.223919 ns Found 3 outliers in 60 samples (5.0000 %) low-severe 1 (1.6667 %) low-mild 2 (3.3333 %) Variance from outliers : 56.7807 % Variance is severely inflated by outliers
This gives you several different statistics on the measured performance of the code within
I measured the time it took for the following functions on java.lang.Math and Jafama FastMath. The times are in mean nanoseconds as reported by Criterium. The times were measured with Java 8 and Jafama 2.1.
|Function||Math Mean||FastMath Mean||Times Faster|
The results clearly show FastMath having an edge on Java Math for many of the functions especially the trigonometric functions and the hypot function (used for calculating the length of the hypotenuse of a triangle).
Some of the functions like ceil, floor, toDegrees, and toRadians are basically equal to that of Java Math. It’s best to use the Math implementation for those.
The sqrt function is actually slower than the equivalent Math function. The Jafama source code of sqrt, log, and log10 functions by default delegate to java.lang.Math implementations saying they “seem usually fast.” You can override that and use the Jafama implementations by setting system properties
jafama.fastsqrt to true. I set those properties to true for the performance and accuracy test to see how they compared. Given the performance of these functions it seems best to stick with the defaults.
I measured the accuracy of all the given functions by searching for the worst case difference between a FastMath function and the equivalent Java Math function. A random distribution of 10,000 numbers was used. Smaller is better.
The difference is calculated by first getting the absolute value of the difference between the FastMath and Math results. Then that result is converted to a percentage of the Math result. It’s done as a percentage because certain functions like pow or exp can generate huge results depending on the input. The FastMath and Math results might be proportionally close to each other but still generate a huge number. The accuracy of Java doubles could play a factor as well. As the Java Doubles get larger the observable difference between successive doubles is larger. The difference between MAX_DOUBLE and the next smallest observable double is about 2.0E292 for example. A percentage difference helps account for that.
|Function||Worst Case % Difference|
Other than the places where the results are exactly the same (a result of 0.0) the percentage difference here is not quite as good as the claimed 1e-15 accuracy. However, it was said to be “about” that accurate and the accuracy may have been measured more as an actual difference rather than a percentage.
Jafama claims “2-4 (up to 15) times faster, with about 1e-15 accuracy.” It mostly lives up to these claim depending on the functions being used. You’ll have to consider carefully whether the trade off is worth it for your own application. Errors can accumulate more if you’re making multiple calls with these functions to build your results. You should test FastMath and Math in your own application and find out for yourself.