1 /* 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 17 package benchmarks.regression; 18 19 import com.google.caliper.BeforeExperiment; 20 import java.lang.reflect.Field; 21 import java.lang.reflect.Method; 22 23 public 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 setUp()32 protected void setUp() throws Exception { 33 setX = View.class.getDeclaredMethod("setX", float.class); 34 x = View.class.getDeclaredField("x"); 35 } 36 timeDirectSetter(int reps)37 public void timeDirectSetter(int reps) { 38 for (int i = 0; i < reps; i++) { 39 view.setX(0.1f); 40 } 41 } 42 timeDirectFieldSet(int reps)43 public void timeDirectFieldSet(int reps) { 44 for (int i = 0; i < reps; i++) { 45 view.x = 0.1f; 46 } 47 } 48 timeDirectSetterAndBoxing(int reps)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 timeDirectFieldSetAndBoxing(int reps)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 timeReflectionSetterAndTwoBoxes(int reps)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 timeReflectionSetterAndOneBox(int reps)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 timeReflectionFieldSet(int reps)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 timeGeneratedSetter(int reps)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 timeGeneratedFieldSet(int reps)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 setX(float x)97 public void setX(float x) { 98 this.x = x; 99 } 100 } 101 102 static interface GeneratedProperty { setFloat(View v, float f)103 void setFloat(View v, float f); 104 } 105 106 static class GeneratedSetter implements GeneratedProperty { setFloat(View v, float f)107 public void setFloat(View v, float f) { 108 v.setX(f); 109 } 110 } 111 112 static class GeneratedField implements GeneratedProperty { setFloat(View v, float f)113 public void setFloat(View v, float f) { 114 v.x = f; 115 } 116 } 117 } 118