Home | History | Annotate | Download | only in benchmarks
      1 /*
      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 
     17 package benchmarks;
     18 
     19 /**
     20  * What does field access cost?
     21  */
     22 public class FieldAccessBenchmark {
     23     private static class Inner {
     24         public int publicInnerIntVal;
     25         protected int protectedInnerIntVal;
     26         private int privateInnerIntVal;
     27         int packageInnerIntVal;
     28     }
     29     int intVal = 42;
     30     final int finalIntVal = 42;
     31     static int staticIntVal = 42;
     32     static final int staticFinalIntVal = 42;
     33     public int timeField(int reps) {
     34         int result = 0;
     35         for (int rep = 0; rep < reps; ++rep) {
     36             result = intVal;
     37         }
     38         return result;
     39     }
     40     public int timeFieldFinal(int reps) {
     41         int result = 0;
     42         for (int rep = 0; rep < reps; ++rep) {
     43             result = finalIntVal;
     44         }
     45         return result;
     46     }
     47     public int timeFieldStatic(int reps) {
     48         int result = 0;
     49         for (int rep = 0; rep < reps; ++rep) {
     50             result = staticIntVal;
     51         }
     52         return result;
     53     }
     54     public int timeFieldStaticFinal(int reps) {
     55         int result = 0;
     56         for (int rep = 0; rep < reps; ++rep) {
     57             result = staticFinalIntVal;
     58         }
     59         return result;
     60     }
     61     public int timeFieldCached(int reps) {
     62         int result = 0;
     63         int cachedIntVal = this.intVal;
     64         for (int rep = 0; rep < reps; ++rep) {
     65             result = cachedIntVal;
     66         }
     67         return result;
     68     }
     69     public int timeFieldPrivateInnerClassPublicField(int reps) {
     70         int result = 0;
     71         Inner inner = new Inner();
     72         for (int rep = 0; rep < reps; ++rep) {
     73             result = inner.publicInnerIntVal;
     74         }
     75         return result;
     76     }
     77     public int timeFieldPrivateInnerClassProtectedField(int reps) {
     78         int result = 0;
     79         Inner inner = new Inner();
     80         for (int rep = 0; rep < reps; ++rep) {
     81             result = inner.protectedInnerIntVal;
     82         }
     83         return result;
     84     }
     85     public int timeFieldPrivateInnerClassPrivateField(int reps) {
     86         int result = 0;
     87         Inner inner = new Inner();
     88         for (int rep = 0; rep < reps; ++rep) {
     89             result = inner.privateInnerIntVal;
     90         }
     91         return result;
     92     }
     93     public int timeFieldPrivateInnerClassPackageField(int reps) {
     94         int result = 0;
     95         Inner inner = new Inner();
     96         for (int rep = 0; rep < reps; ++rep) {
     97             result = inner.packageInnerIntVal;
     98         }
     99         return result;
    100     }
    101 }
    102