diff options
Diffstat (limited to 'src/main/java/org/linaro/benchmarks/jit_aot/Invoke.java')
-rw-r--r-- | src/main/java/org/linaro/benchmarks/jit_aot/Invoke.java | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/src/main/java/org/linaro/benchmarks/jit_aot/Invoke.java b/src/main/java/org/linaro/benchmarks/jit_aot/Invoke.java new file mode 100644 index 0000000..36a4f76 --- /dev/null +++ b/src/main/java/org/linaro/benchmarks/jit_aot/Invoke.java @@ -0,0 +1,92 @@ +/* + * Copyright (C) 2016 Linaro Limited. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* + * This benchmark is inspired by benchmarks/caffeinemark. + * Original benchmark implements a recursion method which calls itself with invoke-virtual. + * + * This behavior can be different on JIT and AOT mode, because: + * - JIT mode can optimize invoke-virtual with inline cache mechanism. + * - AOT mode has no such optimization. + * + * This benchmark exposes such difference between ART JIT and AOT mode. + */ + +package org.linaro.benchmarks.jit_aot; + +import org.openjdk.jmh.annotations.*; +import java.util.concurrent.TimeUnit; + +@BenchmarkMode(Mode.AverageTime) +@OutputTimeUnit(TimeUnit.NANOSECONDS) +@State(Scope.Benchmark) + +public class Invoke { + + public int recursionInvokeVirtual(int i) { + if (i == 0) { + return 0; + } else { + return i + recursionInvokeVirtual(i - 1); + } + } + + public static int recursionInvokeStatic(int i) { + if (i == 0) { + return 0; + } else { + return i + recursionInvokeStatic(i - 1); + } + } + + public final int recursionInvokeFinal(int i) { + if (i == 0) { + return 0; + } else { + return i + recursionInvokeFinal(i - 1); + } + } + + private int recursionInvokePrivate(int i) { + if (i == 0) { + return 0; + } else { + return i + recursionInvokePrivate(i - 1); + } + } + + private static final int recursion_depth = 1000; + + @Benchmark + public void jmhTimeRecursionInvokeVirtual() { + recursionInvokeVirtual(recursion_depth); + } + + @Benchmark + public void jmhTimeRecursionInvokeStatic() { + recursionInvokeStatic(recursion_depth); + } + + @Benchmark + public void jmhTimeRecursionInvokeFinal() { + recursionInvokeFinal(recursion_depth); + } + + @Benchmark + public void jmhTimeRecursionInvokePrivate() { + recursionInvokePrivate(recursion_depth); + } +} |