Home | History | Annotate | Download | only in collect
      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 com.google.common.collect.HashMultiset;
     20 import com.google.common.collect.Multiset;
     21 
     22 import java.util.ArrayList;
     23 import java.util.List;
     24 import java.util.Random;
     25 
     26 /**
     27  * Benchmark for HashMultiset.add for an already-present element.
     28  *
     29  * @author Louis Wasserman
     30  */
     31 public class HashMultisetAddPresentBenchmark {
     32   private static final int ARRAY_MASK = 0x0ffff;
     33   private static final int ARRAY_SIZE = 0x10000;
     34   List<Multiset<Integer>> multisets = new ArrayList<Multiset<Integer>>(0x10000);
     35   int[] queries = new int[ARRAY_SIZE];
     36 
     37   @BeforeExperiment
     38   void setUp() {
     39     Random random = new Random();
     40     multisets.clear();
     41     for (int i = 0; i < ARRAY_SIZE; i++) {
     42       HashMultiset<Integer> multiset = HashMultiset.<Integer>create();
     43       multisets.add(multiset);
     44       queries[i] = random.nextInt();
     45       multiset.add(queries[i]);
     46     }
     47   }
     48 
     49   @Benchmark int add(int reps) {
     50     int tmp = 0;
     51     for (int i = 0; i < reps; i++) {
     52       int j = i & ARRAY_MASK;
     53       tmp += multisets.get(j).add(queries[j], 4);
     54     }
     55     return tmp;
     56   }
     57 }
     58