Answer the question
In order to leave comments, you need to log in
How to optimize static methods + parallel computing issues?
There is a MyMath class, which implements mathematical calculations in the form of static methods. Each of the static methods receives a parameter and uses only it or the method's local primitive variables during calculations (i.e., it is thread-safe). Some of the methods take quite a long time to calculate (some even within a few seconds).
These methods are expected to be called from many different threads. These threads are created in different places like:
Runnable myTask = () -> { ... у1=MyMath.calc1(x1); ... уN=MyMath.calcN(xN) ...};
new Thread(myTask).start()
; class MyMAth {
public static double myCalc(double x) {
double res=0;
for(i=0;i< 1000000;i++)
res += страшная матан формула, зависящая от i и от x;
return res;
}
}
Answer the question
In order to leave comments, you need to log in
Creating a thread is expensive, it's better to always have a pool (if you can roughly estimate how many threads you need, then FixedThreadPool, if not, then let the JVM choose and use CachedThreadPool). Regarding inline-methods, it has already been discussed a million times, the JVM is pretty smart to decide whether to inline methods, you just need to warm it up. Information can be obtained from here . More specifically, this paragraph:
Method Inlining
The frequency of virtual method invocations in the Java programming language is an important optimization bottleneck. Once the Java HotSpot adaptive optimizer has gathered information during execution about program hot spots, it not only compiles the hot spot into native code, but also performs extensive method inlining on that code.
Inlining has important benefits. It dramatically reduces the dynamic frequency of method invocations, which saves the time needed to perform those method invocations. But even more importantly, inlining produces much larger blocks of code for the optimizer to work on. This creates a situation that significantly increases the effectiveness of traditional compiler optimizations, overcoming a major obstacle to increased Java programming language performance.
Inlining is synergistic with other code optimizations, because it makes them more effective. As the Java HotSpot compiler matures, the ability to operate on large, inlined blocks of code will open the door to a host of even more advanced optimizations in the future.
If your task is executed without blocks / interruptions to wait for external actions, then you will not be able to influence the operation of the processor / JVM and everything else, in this case I suggest forgetting about processors / registers / etc.
For the rest, read the documentation, find out what compiles into what and how it works, it's too long to describe it here.
Good luck.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question