• 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"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the
10  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11  * express or implied. See the License for the specific language governing permissions and
12  * limitations under the License.
13  */
14 
15 package com.google.common.collect;
16 
17 import com.google.caliper.BeforeExperiment;
18 import com.google.caliper.Benchmark;
19 import java.util.ArrayList;
20 import java.util.List;
21 import java.util.Random;
22 
23 /**
24  * Benchmark for HashMultiset.add for an already-present element.
25  *
26  * @author Louis Wasserman
27  */
28 public class HashMultisetAddPresentBenchmark {
29   private static final int ARRAY_MASK = 0x0ffff;
30   private static final int ARRAY_SIZE = 0x10000;
31   List<Multiset<Integer>> multisets = new ArrayList<>(0x10000);
32   int[] queries = new int[ARRAY_SIZE];
33 
34   @BeforeExperiment
setUp()35   void setUp() {
36     Random random = new Random();
37     multisets.clear();
38     for (int i = 0; i < ARRAY_SIZE; i++) {
39       HashMultiset<Integer> multiset = HashMultiset.<Integer>create();
40       multisets.add(multiset);
41       queries[i] = random.nextInt();
42       multiset.add(queries[i]);
43     }
44   }
45 
46   @Benchmark
add(int reps)47   int add(int reps) {
48     int tmp = 0;
49     for (int i = 0; i < reps; i++) {
50       int j = i & ARRAY_MASK;
51       tmp += multisets.get(j).add(queries[j], 4);
52     }
53     return tmp;
54   }
55 }
56