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  * Compares various kinds of method invocation.
     21  */
     22 public class MethodInvocationBenchmark {
     23     interface I {
     24         void emptyInterface();
     25     }
     26 
     27     static class C implements I {
     28         private int field;
     29 
     30         private int getField() {
     31             return field;
     32         }
     33 
     34         public int timeInternalGetter(int reps) {
     35             int result = 0;
     36             for (int i = 0; i < reps; ++i) {
     37                 result = getField();
     38             }
     39             return result;
     40         }
     41 
     42         public int timeInternalFieldAccess(int reps) {
     43             int result = 0;
     44             for (int i = 0; i < reps; ++i) {
     45                 result = field;
     46             }
     47             return result;
     48         }
     49 
     50         public static void emptyStatic() {
     51         }
     52 
     53         public void emptyVirtual() {
     54         }
     55 
     56         public void emptyInterface() {
     57         }
     58     }
     59 
     60     public void timeInternalGetter(int reps) {
     61         new C().timeInternalGetter(reps);
     62     }
     63 
     64     public void timeInternalFieldAccess(int reps) {
     65         new C().timeInternalFieldAccess(reps);
     66     }
     67 
     68     // Test an intrinsic.
     69     public int timeStringLength(int reps) {
     70         int result = 0;
     71         for (int i = 0; i < reps; ++i) {
     72             result = "hello, world!".length();
     73         }
     74         return result;
     75     }
     76 
     77     public void timeEmptyStatic(int reps) {
     78         C c = new C();
     79         for (int i = 0; i < reps; ++i) {
     80             c.emptyStatic();
     81         }
     82     }
     83 
     84     public void timeEmptyVirtual(int reps) {
     85         C c = new C();
     86         for (int i = 0; i < reps; ++i) {
     87             c.emptyVirtual();
     88         }
     89     }
     90 
     91     public void timeEmptyInterface(int reps) {
     92         I c = new C();
     93         for (int i = 0; i < reps; ++i) {
     94             c.emptyInterface();
     95         }
     96     }
     97 
     98     public static class Inner {
     99         private int i;
    100         private void privateMethod() { ++i; }
    101         protected void protectedMethod() { ++i; }
    102         public void publicMethod() { ++i; }
    103         void packageMethod() { ++i; }
    104         final void finalPackageMethod() { ++i; }
    105     }
    106 
    107     public void timePrivateInnerPublicMethod(int reps) {
    108         Inner inner = new Inner();
    109         for (int i = 0; i < reps; ++i) {
    110             inner.publicMethod();
    111         }
    112     }
    113 
    114     public void timePrivateInnerProtectedMethod(int reps) {
    115         Inner inner = new Inner();
    116         for (int i = 0; i < reps; ++i) {
    117             inner.protectedMethod();
    118         }
    119     }
    120 
    121     public void timePrivateInnerPrivateMethod(int reps) {
    122         Inner inner = new Inner();
    123         for (int i = 0; i < reps; ++i) {
    124             inner.privateMethod();
    125         }
    126     }
    127 
    128     public void timePrivateInnerPackageMethod(int reps) {
    129         Inner inner = new Inner();
    130         for (int i = 0; i < reps; ++i) {
    131             inner.packageMethod();
    132         }
    133     }
    134 
    135     public void timePrivateInnerFinalPackageMethod(int reps) {
    136         Inner inner = new Inner();
    137         for (int i = 0; i < reps; ++i) {
    138           inner.finalPackageMethod();
    139         }
    140     }
    141 }
    142