Home | History | Annotate | Download | only in regression
      1 /*
      2  * Copyright (C) 2009 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.regression;
     18 
     19 import com.google.caliper.Param;
     20 
     21 /**
     22  * Tests the performance of various StringBuilder methods.
     23  */
     24 public class StringBuilderBenchmark {
     25 
     26     @Param({"1", "10", "100"}) private int length;
     27 
     28     public void timeAppendBoolean(int reps) {
     29         for (int i = 0; i < reps; ++i) {
     30             StringBuilder sb = new StringBuilder();
     31             for (int j = 0; j < length; ++j) {
     32                 sb.append(true);
     33             }
     34         }
     35     }
     36 
     37     public void timeAppendChar(int reps) {
     38         for (int i = 0; i < reps; ++i) {
     39             StringBuilder sb = new StringBuilder();
     40             for (int j = 0; j < length; ++j) {
     41                 sb.append('c');
     42             }
     43         }
     44     }
     45 
     46     public void timeAppendCharArray(int reps) {
     47         char[] chars = "chars".toCharArray();
     48         for (int i = 0; i < reps; ++i) {
     49             StringBuilder sb = new StringBuilder();
     50             for (int j = 0; j < length; ++j) {
     51                 sb.append(chars);
     52             }
     53         }
     54     }
     55 
     56     public void timeAppendCharSequence(int reps) {
     57         CharSequence cs = "chars";
     58         for (int i = 0; i < reps; ++i) {
     59             StringBuilder sb = new StringBuilder();
     60             for (int j = 0; j < length; ++j) {
     61                 sb.append(cs);
     62             }
     63         }
     64     }
     65 
     66     public void timeAppendSubCharSequence(int reps) {
     67         CharSequence cs = "chars";
     68         for (int i = 0; i < reps; ++i) {
     69             StringBuilder sb = new StringBuilder();
     70             for (int j = 0; j < length; ++j) {
     71                 sb.append(cs);
     72             }
     73         }
     74     }
     75 
     76     public void timeAppendDouble(int reps) {
     77         double d = 1.2;
     78         for (int i = 0; i < reps; ++i) {
     79             StringBuilder sb = new StringBuilder();
     80             for (int j = 0; j < length; ++j) {
     81                 sb.append(d);
     82             }
     83         }
     84     }
     85 
     86     public void timeAppendFloat(int reps) {
     87         float f = 1.2f;
     88         for (int i = 0; i < reps; ++i) {
     89             StringBuilder sb = new StringBuilder();
     90             for (int j = 0; j < length; ++j) {
     91                 sb.append(f);
     92             }
     93         }
     94     }
     95 
     96     public void timeAppendInt(int reps) {
     97         int n = 123;
     98         for (int i = 0; i < reps; ++i) {
     99             StringBuilder sb = new StringBuilder();
    100             for (int j = 0; j < length; ++j) {
    101                 sb.append(n);
    102             }
    103         }
    104     }
    105 
    106     public void timeAppendLong(int reps) {
    107         long l = 123;
    108         for (int i = 0; i < reps; ++i) {
    109             StringBuilder sb = new StringBuilder();
    110             for (int j = 0; j < length; ++j) {
    111                 sb.append(l);
    112             }
    113         }
    114     }
    115 
    116     public void timeAppendObject(int reps) {
    117         // We don't want to time the toString, so ensure we're calling a trivial one...
    118         Object o = new Object() {
    119             @Override public String toString() {
    120                 return "constant";
    121             }
    122         };
    123         for (int i = 0; i < reps; ++i) {
    124             StringBuilder sb = new StringBuilder();
    125             for (int j = 0; j < length; ++j) {
    126                 sb.append(o);
    127             }
    128         }
    129     }
    130 
    131     public void timeAppendString(int reps) {
    132         String s = "chars";
    133         for (int i = 0; i < reps; ++i) {
    134             StringBuilder sb = new StringBuilder();
    135             for (int j = 0; j < length; ++j) {
    136                 sb.append(s);
    137             }
    138         }
    139     }
    140 }
    141