我目前正在尝试 java Ahead of Time 功能。
我执行以下代码:
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);
}
}
}
我启动:
[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)
仅需 3267 毫秒
现在我决定用 AOT 功能编译相同的代码
[baptiste@KEYWER tuto_aot]$ $JAVA_HOME/bin/jaotc --output CountUppercase.so CountUppercase.class
然后我运行 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)
它需要 5 秒,因此比经典执行多 2 秒!
我不明白为什么我会得到这种行为?
谢谢你的时间