Can I reproduce some of the optimizations made by the JVM manually?

I am working on a Sudoku solver in school, and we have a little competition. Now my algorithm is pretty fast in the first run (about 2.5 ms), but even faster when I solve the same puzzle 10,000 times (about 0.5 ms for each run). Of course, this time depends on the puzzle being solved. I know that the JVM does some optimization when the method is called several times, and this is what I suspect is happening.

I don’t think that I can further optimize the algorithm itself (although I will continue to search), so I was wondering if I could reproduce some of the optimizations performed by the JVM.

Note: compiling to native code is not an option.

Thank!

Edit: All these parameters of the virtual machine are good, but are not really β€œlegal” in the competition of algorithms, since everyone can use these parameters and increase productivity. I am looking for code optimization.

+3
source share
9 answers

tl; dr : no, most optimizations performed by the JVM cannot be expressed in Java bytecode.

At its core, Java byte code defines your code at a very high level.

This is by design and is one of the reasons why the JVM can perform all these optimizations: the byte code describes the operations at a relatively high level and leaves the actual data on the JVM's execution.

.

, JVM , , , ArrayIndexOutOfBoundsException (. VM Spec 2.5.14).

JVM , , (, , JVM ).

, , .

+1

, . JavaWorld . .

. , .

+3

"", JIT ,

-XX:CompileThreshold=20

, . ( ) .

+2

, . , "" , .

+1

"" , , `System.gc();

0

JVM JIT (Just-In-Time): - Java . , , . , , - , - 10 000 , , JVM, , , , , .

, , JVM . (edit: , mdma - -XX).

Sun JVM: JVM, , , JVM, , .

JVM :

java -server com.mypackage.MyProgram

, , , 64- Sun Java Windows JVM .

0

-, , , - , , . .

, ;)

0

, , , (, ). (SPEC ..) ( ), , , , JIT ..

, , -, 2,5 , - JVM , Windows, , , System.currentTimeMillis(). , ... 30 60 .

- , . FYI , . .

0

, , :

1) , . 2

int i = 10 / 2;  // division by 2
i     = 10 >> 1; // does the same

2) , . int over Integer

3)

4) , . + = 2 = + 2

5) , ,

for (int i = 10; i >= 0; i--) {
    System.out.println(i);
}

(only useful if you can compare against zero)

6)

0

Source: https://habr.com/ru/post/1747851/


All Articles