0

I'm currently trying java Ahead of Time feature.

I execute the following code:

public class CountUppercase {
    static final int ITERATIONS = Math.max(Integer.getInteger("iterations", 1), 1);
    public static void main(String[] args) {
        String sentence = String.join(" ", args);
        for (int iter = 0; iter < ITERATIONS; iter++) {
            if (ITERATIONS != 1) System.out.println("-- iteration " + (iter + 1) + " --");
            long total = 0, start = System.currentTimeMillis(), last = start;
            for (int i = 1; i < 10_000_000; i++) {
                total += sentence.chars().filter(Character::isUpperCase).count();
                if (i % 1_000_000 == 0) {
                    long now = System.currentTimeMillis();
                    System.out.printf("%d (%d ms)%n", i / 1_000_000, now - last);
                    last = now;
                }
            }
            System.out.printf("total: %d (%d ms)%n", total, System.currentTimeMillis() - start);
        }
    }
}

I Launch:

[baptiste@KEYWER tuto_aot]$ $JAVA_HOME/bin/java -version
java version "13.0.1" 2019-10-15
Java(TM) SE Runtime Environment (build 13.0.1+9)
Java HotSpot(TM) 64-Bit Server VM (build 13.0.1+9, mixed mode, sharing)
[baptiste@KEYWER tuto_aot]$ $JAVA_HOME/bin/javac CountUppercase.java 
[baptiste@KEYWER tuto_aot]$ $JAVA_HOME/bin/java CountUppercase In 2020 I would like to run ALL languages in one VM.
1 (351 ms)
2 (359 ms)
3 (316 ms)
4 (342 ms)
5 (316 ms)
6 (318 ms)
7 (313 ms)
8 (316 ms)
9 (320 ms)
total: 69999993 (3267 ms)

It take only 3267 ms

Now I decide to compile the same code with AOT feature

[baptiste@KEYWER tuto_aot]$ $JAVA_HOME/bin/jaotc --output CountUppercase.so CountUppercase.class 

Then I run the AOT:

[baptiste@KEYWER tuto_aot]$ $JAVA_HOME/bin/java -XX:AOTLibrary=./CountUppercase.so CountUppercase In 2020 I would like to run ALL languages in one VM.
1 (581 ms)
2 (503 ms)
3 (486 ms)
4 (505 ms)
5 (483 ms)
6 (486 ms)
7 (483 ms)
8 (490 ms)
9 (481 ms)
total: 69999993 (4981 ms)

It takes 5 seconds so around 2 seconds more than classical execution !
I don't understand why I get this behavior ?

Thx for your time

bmeynier
  • 289
  • 2
  • 12
  • This is a similar question (maybe even a duplicate): https://stackoverflow.com/questions/1878696/why-is-java-faster-when-using-a-jit-vs-compiling-to-machine-code – user May 14 '20 at 17:11
  • Thx for your awnser.I test the same code with GraalVM with native-compilation feature and the result is worse, the execution take 7052 ms – bmeynier May 14 '20 at 17:59
  • Graal's native images aren't quite as good as a warm JVM, probably because of how many years of experience the JVM has optimizing code – user May 14 '20 at 18:00
  • I get list of limitations for GraalVM AOT https://github.com/oracle/graal/blob/master/substratevm/LIMITATIONS.md, I don't see any reflexion nor Dynamic Class Loading in the code – bmeynier May 14 '20 at 18:03
  • I think the issue is the System.currentTimeMillis() instruction – bmeynier May 14 '20 at 21:59

0 Answers0