blob: 2781a29017ebe7cbdc9fe24cdb806bff842b7cec [file] [log] [blame]
Alan Viverette3da604b2020-06-10 18:34:39 +00001/*
2 * Copyright (C) 2011 The Android Open Source Project
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.regression;
18
19import com.google.caliper.BeforeExperiment;
20import java.lang.reflect.Field;
21import java.lang.reflect.Method;
22
23public final class PropertyAccessBenchmark {
24 private View view = new View();
25 private Method setX;
26 private GeneratedProperty generatedSetter = new GeneratedSetter();
27 private GeneratedProperty generatedField = new GeneratedField();
28 private Field x;
29 private Object[] argsBox = new Object[1];
30
31 @BeforeExperiment
32 protected void setUp() throws Exception {
33 setX = View.class.getDeclaredMethod("setX", float.class);
34 x = View.class.getDeclaredField("x");
35 }
36
37 public void timeDirectSetter(int reps) {
38 for (int i = 0; i < reps; i++) {
39 view.setX(0.1f);
40 }
41 }
42
43 public void timeDirectFieldSet(int reps) {
44 for (int i = 0; i < reps; i++) {
45 view.x = 0.1f;
46 }
47 }
48
49 public void timeDirectSetterAndBoxing(int reps) {
50 for (int i = 0; i < reps; i++) {
51 Float value = 0.1f;
52 view.setX(value);
53 }
54 }
55
56 public void timeDirectFieldSetAndBoxing(int reps) {
57 for (int i = 0; i < reps; i++) {
58 Float value = 0.1f;
59 view.x = value;
60 }
61 }
62
63 public void timeReflectionSetterAndTwoBoxes(int reps) throws Exception {
64 for (int i = 0; i < reps; i++) {
65 setX.invoke(view, 0.1f);
66 }
67 }
68
69 public void timeReflectionSetterAndOneBox(int reps) throws Exception {
70 for (int i = 0; i < reps; i++) {
71 argsBox[0] = 0.1f;
72 setX.invoke(view, argsBox);
73 }
74 }
75
76 public void timeReflectionFieldSet(int reps) throws Exception {
77 for (int i = 0; i < reps; i++) {
78 x.setFloat(view, 0.1f);
79 }
80 }
81
82 public void timeGeneratedSetter(int reps) throws Exception {
83 for (int i = 0; i < reps; i++) {
84 generatedSetter.setFloat(view, 0.1f);
85 }
86 }
87
88 public void timeGeneratedFieldSet(int reps) throws Exception {
89 for (int i = 0; i < reps; i++) {
90 generatedField.setFloat(view, 0.1f);
91 }
92 }
93
94 static class View {
95 float x;
96
97 public void setX(float x) {
98 this.x = x;
99 }
100 }
101
102 static interface GeneratedProperty {
103 void setFloat(View v, float f);
104 }
105
106 static class GeneratedSetter implements GeneratedProperty {
107 public void setFloat(View v, float f) {
108 v.setX(f);
109 }
110 }
111
112 static class GeneratedField implements GeneratedProperty {
113 public void setFloat(View v, float f) {
114 v.x = f;
115 }
116 }
117}