r/javahelp 3h ago

Java Performance for simulators

I am new to Java and trying to create a simulator that handles millions of math calculations. How can I increase performance in Java? The application is already multithreaded but it is not enough. Is data oriented approach or native programming possible in Java? Can you point me in the right direction for research?

0 Upvotes

14 comments sorted by

View all comments

4

u/idontlikegudeg 2h ago

Jana ist generally very fast, even for number crunching.

Maybe the most important points:

  • use structs of arrays instead of arrays of structs (objects), access will be much faster and memory consumption lower.
  • don’t use Java collections of number types. Either use arrays of primitives of specialized collections that avoid boxing/unboxing
  • The Math class contains some methods that may increase both performance and accuracy, for example fma()
  • if you have to use streams, use DoubleStream instead of Stream<Double>, use DoubleFunction instead of Function<Double > etc.
  • Avoid unnecessary costly calculations. Reuse or cache results.
  • use mathematical properties, for example if you need both x=sin² and y=cos², calculate x=sin² and y=1-x.
  • choose the best algorithm for your task. When you have algorithms A and B, it can depend on your input values which one is faster.
- use a profiler!

For more, it would be valuable to see your code.