Home | History | Annotate | Download | only in collect
      1 /*
      2  * Copyright (C) 2012 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.collect.BenchmarkHelpers.SetImpl;
     23 import com.google.common.collect.CollectionBenchmarkSampleData.Element;
     24 
     25 import java.util.Set;
     26 
     27 /**
     28  * Test iteration speed at various size for {@link Set} instances.
     29  *
     30  * @author Christopher Swenson
     31  */
     32 public class SetIterationBenchmark {
     33   @Param({ "3", "6", "11", "23", "45", "91", "181", "362", "724", "1448",
     34       "2896", "5793", "11585", "23170", "46341", "92682", "185364", "370728",
     35       "741455", "1482910", "2965821", "5931642"})
     36   private int size;
     37 
     38   // "" means no fixed seed
     39   @Param("1234")
     40   private SpecialRandom random;
     41 
     42   @Param({"Immutable", "Hash"})
     43   private SetImpl impl;
     44 
     45   // the following must be set during setUp
     46   private Set<Element> setToTest;
     47 
     48   @BeforeExperiment void setUp() {
     49     CollectionBenchmarkSampleData sampleData =
     50         new CollectionBenchmarkSampleData(true, random, 0.8, size);
     51     setToTest = impl.create(sampleData.getValuesInSet());
     52   }
     53 
     54   @Benchmark int iteration(int reps) {
     55     int x = 0;
     56 
     57     for (int i = 0; i < reps; i++) {
     58       for (Element y : setToTest) {
     59         x ^= System.identityHashCode(y);
     60       }
     61     }
     62     return x;
     63   }
     64 }
     65