Home | History | Annotate | Download | only in examples
      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 examples;
     18 
     19 import com.google.caliper.Benchmark;
     20 import java.text.DecimalFormatSymbols;
     21 import java.text.NumberFormat;
     22 import java.text.SimpleDateFormat;
     23 import java.util.Locale;
     24 
     25 /**
     26  * Benchmarks creation and cloning various expensive objects.
     27  */
     28 @SuppressWarnings({"ResultOfObjectAllocationIgnored"}) // TODO: should fix!
     29 public class ExpensiveObjectsBenchmark {
     30   @Benchmark void newDecimalFormatSymbols(int reps) {
     31     for (int i = 0; i < reps; ++i) {
     32       new DecimalFormatSymbols(Locale.US);
     33     }
     34   }
     35 
     36   @Benchmark void clonedDecimalFormatSymbols(int reps) {
     37     DecimalFormatSymbols dfs = new DecimalFormatSymbols(Locale.US);
     38     for (int i = 0; i < reps; ++i) {
     39       dfs.clone();
     40     }
     41   }
     42 
     43   @Benchmark void newNumberFormat(int reps) {
     44     for (int i = 0; i < reps; ++i) {
     45       NumberFormat.getInstance(Locale.US);
     46     }
     47   }
     48 
     49   @Benchmark void clonedNumberFormat(int reps) {
     50     NumberFormat nf = NumberFormat.getInstance(Locale.US);
     51     for (int i = 0; i < reps; ++i) {
     52       nf.clone();
     53     }
     54   }
     55 
     56   @Benchmark void newSimpleDateFormat(int reps) {
     57     for (int i = 0; i < reps; ++i) {
     58       new SimpleDateFormat();
     59     }
     60   }
     61 
     62   @Benchmark void clonedSimpleDateFormat(int reps) {
     63     SimpleDateFormat sdf = new SimpleDateFormat();
     64     for (int i = 0; i < reps; ++i) {
     65       sdf.clone();
     66     }
     67   }
     68 }
     69