1 /*
2  * Copyright (C) 2010 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.collect;
18 
19 import com.google.caliper.BeforeExperiment;
20 import com.google.caliper.Benchmark;
21 import com.google.caliper.Param;
22 import com.google.common.base.Preconditions;
23 import java.util.Random;
24 
25 /**
26  * Tests the speed of iteration of different iteration methods for collections.
27  *
28  * @author David Richter
29  */
30 public class MultisetIteratorBenchmark {
31   @Param({"0", "1", "16", "256", "4096", "65536"})
32   int size;
33 
34   LinkedHashMultiset<Object> linkedHashMultiset;
35   HashMultiset<Object> hashMultiset;
36 
37   // TreeMultiset requires a Comparable element.
38   TreeMultiset<Integer> treeMultiset;
39 
40   @BeforeExperiment
setUp()41   void setUp() {
42     hashMultiset = HashMultiset.create(size);
43     linkedHashMultiset = LinkedHashMultiset.create(size);
44     treeMultiset = TreeMultiset.create();
45 
46     Random random = new Random();
47 
48     int sizeRemaining = size;
49 
50     // TODO(kevinb): generate better test contents for multisets
51     for (int i = 0; sizeRemaining > 0; i++) {
52       // The JVM will return interned values for small ints.
53       Integer value = random.nextInt(1000) + 128;
54       int count = Math.min(random.nextInt(10) + 1, sizeRemaining);
55       sizeRemaining -= count;
56       hashMultiset.add(value, count);
57       linkedHashMultiset.add(value, count);
58       treeMultiset.add(value, count);
59     }
60 
61     // TODO(kevinb): convert to assert once benchmark tests enable asserts by default
62     Preconditions.checkState(hashMultiset.size() == size);
63   }
64 
65   @Benchmark
hashMultiset(int reps)66   int hashMultiset(int reps) {
67     int sum = 0;
68     for (int i = 0; i < reps; i++) {
69       for (Object value : hashMultiset) {
70         sum += value.hashCode();
71       }
72     }
73     return sum;
74   }
75 
76   @Benchmark
linkedHashMultiset(int reps)77   int linkedHashMultiset(int reps) {
78     int sum = 0;
79     for (int i = 0; i < reps; i++) {
80       for (Object value : linkedHashMultiset) {
81         sum += value.hashCode();
82       }
83     }
84     return sum;
85   }
86 
87   @Benchmark
treeMultiset(int reps)88   int treeMultiset(int reps) {
89     int sum = 0;
90     for (int i = 0; i < reps; i++) {
91       for (Object value : treeMultiset) {
92         sum += value.hashCode();
93       }
94     }
95     return sum;
96   }
97 }
98