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.Runner;
     20 import com.google.caliper.SimpleBenchmark;
     21 import java.util.Formatter;
     22 import java.util.Locale;
     23 
     24 /**
     25  * Compares Formatter against hand-written StringBuilder code.
     26  */
     27 public class FormatterBenchmark extends SimpleBenchmark {
     28     public void timeFormatter_NoFormatting(int reps) {
     29         for (int i = 0; i < reps; i++) {
     30             Formatter f = new Formatter();
     31             f.format("this is a reasonably short string that doesn't actually need any formatting");
     32         }
     33     }
     34 
     35     public void timeStringBuilder_NoFormatting(int reps) {
     36         for (int i = 0; i < reps; i++) {
     37             StringBuilder sb = new StringBuilder();
     38             sb.append("this is a reasonably short string that doesn't actually need any formatting");
     39         }
     40     }
     41 
     42     public void timeFormatter_OneInt(int reps) {
     43         Integer value = Integer.valueOf(1024); // We're not trying to benchmark boxing here.
     44         for (int i = 0; i < reps; i++) {
     45             Formatter f = new Formatter();
     46             f.format("this is a reasonably short string that has an int %d in it", value);
     47         }
     48     }
     49 
     50     public void timeFormatter_OneIntArabic(int reps) {
     51         Locale arabic = new Locale("ar");
     52         Integer value = Integer.valueOf(1024); // We're not trying to benchmark boxing here.
     53         for (int i = 0; i < reps; i++) {
     54             Formatter f = new Formatter();
     55             f.format(arabic, "this is a reasonably short string that has an int %d in it", value);
     56         }
     57     }
     58 
     59     public void timeStringBuilder_OneInt(int reps) {
     60         for (int i = 0; i < reps; i++) {
     61             StringBuilder sb = new StringBuilder();
     62             sb.append("this is a reasonably short string that has an int ");
     63             sb.append(1024);
     64             sb.append(" in it");
     65         }
     66     }
     67 
     68     public void timeFormatter_OneHexInt(int reps) {
     69         Integer value = Integer.valueOf(1024); // We're not trying to benchmark boxing here.
     70         for (int i = 0; i < reps; i++) {
     71             Formatter f = new Formatter();
     72             f.format("this is a reasonably short string that has an int %x in it", value);
     73         }
     74     }
     75 
     76     public void timeStringBuilder_OneHexInt(int reps) {
     77         for (int i = 0; i < reps; i++) {
     78             StringBuilder sb = new StringBuilder();
     79             sb.append("this is a reasonably short string that has an int ");
     80             sb.append(Integer.toHexString(1024));
     81             sb.append(" in it");
     82         }
     83     }
     84 
     85     public void timeFormatter_OneFloat(int reps) {
     86         Float value = Float.valueOf(10.24f); // We're not trying to benchmark boxing here.
     87         for (int i = 0; i < reps; i++) {
     88             Formatter f = new Formatter();
     89             f.format("this is a reasonably short string that has a float %f in it", value);
     90         }
     91     }
     92 
     93     public void timeFormatter_OneFloat_dot2f(int reps) {
     94         Float value = Float.valueOf(10.24f); // We're not trying to benchmark boxing here.
     95         for (int i = 0; i < reps; i++) {
     96             Formatter f = new Formatter();
     97             f.format("this is a reasonably short string that has a float %.2f in it", value);
     98         }
     99     }
    100 
    101     public void timeFormatter_TwoFloats(int reps) {
    102         Float value = Float.valueOf(10.24f); // We're not trying to benchmark boxing here.
    103         for (int i = 0; i < reps; i++) {
    104             Formatter f = new Formatter();
    105             f.format("this is a reasonably short string that has two floats %f and %f in it", value, value);
    106         }
    107     }
    108 
    109     public void timeStringBuilder_OneFloat(int reps) {
    110         for (int i = 0; i < reps; i++) {
    111             StringBuilder sb = new StringBuilder();
    112             sb.append("this is a reasonably short string that has a float ");
    113             sb.append(10.24f);
    114             sb.append(" in it");
    115         }
    116     }
    117 
    118     public void timeFormatter_OneString(int reps) {
    119         for (int i = 0; i < reps; i++) {
    120             Formatter f = new Formatter();
    121             f.format("this is a reasonably short string that has a string %s in it", "hello");
    122         }
    123     }
    124 
    125     public void timeStringBuilder_OneString(int reps) {
    126         for (int i = 0; i < reps; i++) {
    127             StringBuilder sb = new StringBuilder();
    128             sb.append("this is a reasonably short string that has a string ");
    129             sb.append("hello");
    130             sb.append(" in it");
    131         }
    132     }
    133 }
    134