• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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"})
32   int length;
33   // Number of matching strings
34   @Param({"xxxx", "xxXx", "xXxX", "XXXX"})
35   String text;
36 
37   private String input;
38 
39   private static final Splitter CHAR_SPLITTER = Splitter.on('X');
40   private static final Splitter STRING_SPLITTER = Splitter.on("X");
41 
42   @BeforeExperiment
setUp()43   void setUp() {
44     input = Strings.repeat(text, length);
45   }
46 
47   @Benchmark
charSplitter(int reps)48   void charSplitter(int reps) {
49     int total = 0;
50 
51     for (int i = 0; i < reps; i++) {
52       total += Iterables.size(CHAR_SPLITTER.split(input));
53     }
54   }
55 
56   @Benchmark
stringSplitter(int reps)57   void stringSplitter(int reps) {
58     int total = 0;
59 
60     for (int i = 0; i < reps; i++) {
61       total += Iterables.size(STRING_SPLITTER.split(input));
62     }
63   }
64 }
65