Home | History | Annotate | Download | only in base
      1 /*
      2  * Copyright (C) 2011 The Guava Authors
      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 com.google.common.base;
     18 
     19 import com.google.caliper.BeforeExperiment;
     20 import com.google.caliper.Benchmark;
     21 import com.google.caliper.Param;
     22 import com.google.common.collect.Iterables;
     23 
     24 /**
     25  * Microbenchmark for {@link Splitter#on} with char vs String with length == 1.
     26  *
     27  * @author Paul Lindner
     28  */
     29 public class SplitterBenchmark {
     30   // overall size of string
     31   @Param({"1", "10", "100", "1000"}) int length;
     32   // Number of matching strings
     33   @Param({"xxxx", "xxXx", "xXxX", "XXXX"}) String text;
     34 
     35   private String input;
     36 
     37   private static final Splitter CHAR_SPLITTER = Splitter.on('X');
     38   private static final Splitter STRING_SPLITTER = Splitter.on("X");
     39 
     40   @BeforeExperiment void setUp() {
     41     input = Strings.repeat(text, length);
     42   }
     43 
     44   @Benchmark void charSplitter(int reps) {
     45     int total = 0;
     46 
     47     for (int i = 0; i < reps; i++) {
     48       total += Iterables.size(CHAR_SPLITTER.split(input));
     49     }
     50   }
     51 
     52   @Benchmark void stringSplitter(int reps) {
     53     int total = 0;
     54 
     55     for (int i = 0; i < reps; i++) {
     56      total += Iterables.size(STRING_SPLITTER.split(input));
     57     }
     58   }
     59 }
     60