• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2010 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 static com.google.common.collect.DiscreteDomain.integers;
20 
21 import com.google.caliper.BeforeExperiment;
22 import com.google.caliper.Benchmark;
23 import com.google.caliper.Param;
24 import java.util.Set;
25 
26 /**
27  * Very simple powerSet iteration benchmark.
28  *
29  * @author Kevin Bourrillion
30  */
31 public class PowerSetBenchmark {
32   @Param({"2", "4", "8", "16"})
33   int elements;
34 
35   Set<Set<Integer>> powerSet;
36 
37   @BeforeExperiment
setUp()38   void setUp() {
39     Set<Integer> set = ContiguousSet.create(Range.closed(1, elements), integers());
40     powerSet = Sets.powerSet(set);
41   }
42 
43   @Benchmark
iteration(int reps)44   int iteration(int reps) {
45     int sum = 0;
46     for (int i = 0; i < reps; i++) {
47       for (Set<Integer> subset : powerSet) {
48         for (Integer value : subset) {
49           sum += value;
50         }
51       }
52     }
53     return sum;
54   }
55 }
56