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