blob: 7a5e1b6386d4703004e64360d96b845018198ea5 [file] [log] [blame]
Elliott Hughes97aba272013-02-13 16:21:51 -08001/*
2 * Copyright (C) 2010 Google Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package benchmarks;
18
19import com.google.caliper.Param;
20import com.google.caliper.Runner;
21import com.google.caliper.SimpleBenchmark;
22
23/**
24 * Compares various kinds of method invocation.
25 */
26public class MethodInvocationBenchmark extends SimpleBenchmark {
27 interface I {
28 void emptyInterface();
29 }
30
31 static class C implements I {
32 private int field;
33
34 private int getField() {
35 return field;
36 }
37
38 public int timeInternalGetter(int reps) {
39 int result = 0;
40 for (int i = 0; i < reps; ++i) {
41 result = getField();
42 }
43 return result;
44 }
45
46 public int timeInternalFieldAccess(int reps) {
47 int result = 0;
48 for (int i = 0; i < reps; ++i) {
49 result = field;
50 }
51 return result;
52 }
53
54 public static void emptyStatic() {
55 }
56
57 public void emptyVirtual() {
58 }
59
60 public void emptyInterface() {
61 }
62 }
63
64 public void timeInternalGetter(int reps) {
65 new C().timeInternalGetter(reps);
66 }
67
68 public void timeInternalFieldAccess(int reps) {
69 new C().timeInternalFieldAccess(reps);
70 }
71
72 // Test an intrinsic.
73 public int timeStringLength(int reps) {
74 int result = 0;
75 for (int i = 0; i < reps; ++i) {
76 result = "hello, world!".length();
77 }
78 return result;
79 }
80
81 public void timeEmptyStatic(int reps) {
82 C c = new C();
83 for (int i = 0; i < reps; ++i) {
84 c.emptyStatic();
85 }
86 }
87
88 public void timeEmptyVirtual(int reps) {
89 C c = new C();
90 for (int i = 0; i < reps; ++i) {
91 c.emptyVirtual();
92 }
93 }
94
95 public void timeEmptyInterface(int reps) {
96 I c = new C();
97 for (int i = 0; i < reps; ++i) {
98 c.emptyInterface();
99 }
100 }
101
102 public static class Inner {
103 private int i;
104 private void privateMethod() { ++i; }
105 protected void protectedMethod() { ++i; }
106 public void publicMethod() { ++i; }
107 void packageMethod() { ++i; }
108 final void finalPackageMethod() { ++i; }
109 }
110
111 public void timePrivateInnerPublicMethod(int reps) {
112 Inner inner = new Inner();
113 for (int i = 0; i < reps; ++i) {
114 inner.publicMethod();
115 }
116 }
117
118 public void timePrivateInnerProtectedMethod(int reps) {
119 Inner inner = new Inner();
120 for (int i = 0; i < reps; ++i) {
121 inner.protectedMethod();
122 }
123 }
124
125 public void timePrivateInnerPrivateMethod(int reps) {
126 Inner inner = new Inner();
127 for (int i = 0; i < reps; ++i) {
128 inner.privateMethod();
129 }
130 }
131
132 public void timePrivateInnerPackageMethod(int reps) {
133 Inner inner = new Inner();
134 for (int i = 0; i < reps; ++i) {
135 inner.packageMethod();
136 }
137 }
138
139 public void timePrivateInnerFinalPackageMethod(int reps) {
140 Inner inner = new Inner();
141 for (int i = 0; i < reps; ++i) {
142 inner.finalPackageMethod();
143 }
144 }
145}