Java trigonometry performance sucks

January 25, 2006 at 9:46 am | Posted in Programming | 4 Comments

Just did some benchmarking on creating swirlies both in Java and C#. The code is almost completely similar. I create a 200 x 200 pixel image and the results are somewhat shocking: in Java this takes 36.7 seconds while in C# only 10.2 seconds are needed. That’s almost 4 times as fast!
At first I thought I had made some trivial mistake in my code, since I’ve never seen such differences before. However when I looked at other benchmarks, for example this one it soon became clear that Java performance with trigonometry is bad, compared to most other programming languages. And swirlies happen to be full of trigonometric functions.

I hope this will be solved in the coming Mustang release but this is quite unlikely. in this blog it is explained that Java favors accuracy over speed. And one final remark: Mono was only 10 percent slower than .NET 2.0, which is not bad.

4 Comments »

RSS feed for comments on this post. TrackBack URI

  1. Which version of java did you use?

    I’m interested since I’m about to develop a simulator in java, and will be using some trig in distance calculations and such. We are using java 1.5.0_06

  2. I’ve used 1.5.x. If accuracy isn’t the main issue you could consider precalculating a table with sine/cosine values and get the (interpolated) values from this table. That way it will be quite a lot faster.

  3. It will never get resolved. Java has a strict specification on results of trig functions. Since a math processor on an Intel chip is inaccurate then all trigonometry in JVM has to be performed on software. Intel chip is inaccurate when it comes to values much higher than PI. Try an see. Cos(100000000.0f) is different in Java and in C/C++/Assembler

  4. Hi Igor. Thanks for the background. In my blog I have a reference to a blog from James Gosling where he explains why there is such a difference in accuracy.


Leave a reply to maurits Cancel reply

Blog at WordPress.com.
Entries and comments feeds.