Custom math functions vs. supplied Math functions?

2019-03-06 05:13发布

I am basically making a Java program that will have to run a lot of calculations pretty quickly(each frame, aiming for at least 30 f/s). These will mostly be trigonometric and power functions. The question I'm asking is: Which is faster: using the already-supplied-by-Java Math functions? Or writing my own functions to run?

5条回答
Animai°情兽
2楼-- · 2019-03-06 05:32

If you can bear 1e-15ish relative error (or more like 1e-13ish for pow(double,double)), you can try this, which should be faster than java.lang.Math if you call it a lot : http://sourceforge.net/projects/jafama/

As some said, it's usually hard to beat java.lang.Math in pure Java if you want to keep similar (1-ulp-ish) accuracy, but a little bit less accuracy in double precision is often totally bearable (and still much more accurate than what you would have when computing with floats), and can allow for some noticeable speed-up.

查看更多
孤傲高冷的网名
3楼-- · 2019-03-06 05:33

Lots of very intelligent and well-qualified people have put a lot of effort, over many years, into making the Math functions work as quickly and as accurately as possible. So unless you're smarter than all of them, and have years of free time to spend on this, it's very unlikely that you'll be able to do a better job.

Most of them are native too - they're not actually in Java. So writing faster versions of them in Java is going to be a complete no-go. You're probably best off using a mixture of C and Assembly Language when you come to write your own; and you'll need to know all the quirks of whatever hardware you're going to be running this on.

Moreover, the current implementations have been tested over many years, by the fact that millions of people all around the world are using Java in some way. You're not going to have access to the same body of testers, so your functions will automatically be more error-prone than the standard ones. This is unavoidable.

So are you still thinking about writing your own functions?

查看更多
我只想做你的唯一
4楼-- · 2019-03-06 05:38

The built-in Math functions will be extremely difficult to beat, given that most of them have special JVM magic that makes them use hardware intrinsics. You could conceivably beat some of them by trading away accuracy with a lot of work, but you're very unlikely to beat the Math utilities otherwise.

查看更多
我命由我不由天
5楼-- · 2019-03-06 05:45

What might be an option is caching the values. If you know you are only going to need a fixed set of values or if you can get away without perfect accuracy then this could save a lot of time. Say if you want to draw a lot of circles pre compute values of sin and cos for each degree. Then use these values when drawing. Most circles will be small enough that you can't see the difference and the small number which are very big can be done using the libraries.

Be sure to test if this is worth it. On my 5 year old macbook I can do a million evaluations of cos a second.

查看更多
欢心
6楼-- · 2019-03-06 05:47

You will want to use the java.lang.Math functions as most of them run native in the JVM. you can see the source code here.

查看更多
登录 后发表回答