• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
24 /**
25  * This is meant to be used with {@code --measureMemory} to measure the memory usage of various
26  * {@code Set} implementations.
27  *
28  * @author Christopher Swenson
29  */
30 public class SetCreationBenchmark {
31   @Param({
32     "3", "6", "11", "23", "45", "91", "181", "362", "724", "1448", "2896", "5793", "11585", "23170",
33     "46341", "92682", "185364", "370728", "741455", "1482910", "2965821", "5931642"
34   })
35   private int size;
36 
37   // "" means no fixed seed
38   @Param("1234")
39   private SpecialRandom random;
40 
41   @Param({"ImmutableSetImpl", "HashSetImpl"})
42   private SetImpl impl;
43 
44   // the following must be set during setUp
45   private CollectionBenchmarkSampleData sampleData;
46 
47   @BeforeExperiment
setUp()48   void setUp() {
49     sampleData = new CollectionBenchmarkSampleData(true, random, 0.8, size);
50   }
51 
52   @Benchmark
creation(int reps)53   int creation(int reps) {
54     int x = 0;
55     for (int i = 0; i < reps; i++) {
56       x ^= System.identityHashCode(impl.create(sampleData.getValuesInSet()));
57     }
58     return x;
59   }
60 }
61