• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 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.math;
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.ContiguousSet;
23 import com.google.common.collect.DiscreteDomain;
24 import com.google.common.collect.ImmutableSet;
25 import com.google.common.collect.Range;
26 import java.util.Random;
27 
28 /** Benchmarks some algorithms providing the same functionality as {@link Quantiles}. */
29 public class QuantilesBenchmark {
30 
31   private static final ContiguousSet<Integer> ALL_DECILE_INDEXES =
32       ContiguousSet.create(Range.closed(0, 10), DiscreteDomain.integers());
33 
34   @Param({"10", "100", "1000", "10000", "100000"})
35   int datasetSize;
36 
37   @Param QuantilesAlgorithm algorithm;
38 
39   private double[][] datasets = new double[0x100][];
40 
41   @BeforeExperiment
setUp()42   void setUp() {
43     Random rng = new Random();
44     for (int i = 0; i < 0x100; i++) {
45       datasets[i] = new double[datasetSize];
46       for (int j = 0; j < datasetSize; j++) {
47         datasets[i][j] = rng.nextDouble();
48       }
49     }
50   }
51 
dataset(int i)52   private double[] dataset(int i) {
53     // We must test on a fresh clone of the dataset each time. Doing sorts and quickselects on an
54     // dataset which is already sorted or partially sorted is cheating.
55     return datasets[i & 0xFF].clone();
56   }
57 
58   @Benchmark
median(int reps)59   double median(int reps) {
60     double dummy = 0.0;
61     for (int i = 0; i < reps; i++) {
62       dummy += algorithm.singleQuantile(1, 2, dataset(i));
63     }
64     return dummy;
65   }
66 
67   @Benchmark
percentile90(int reps)68   double percentile90(int reps) {
69     double dummy = 0.0;
70     for (int i = 0; i < reps; i++) {
71       dummy += algorithm.singleQuantile(90, 100, dataset(i));
72     }
73     return dummy;
74   }
75 
76   @Benchmark
percentile99(int reps)77   double percentile99(int reps) {
78     double dummy = 0.0;
79     for (int i = 0; i < reps; i++) {
80       dummy += algorithm.singleQuantile(99, 100, dataset(i));
81     }
82     return dummy;
83   }
84 
85   @Benchmark
percentiles90And99(int reps)86   double percentiles90And99(int reps) {
87     double dummy = 0.0;
88     for (int i = 0; i < reps; i++) {
89       dummy += algorithm.multipleQuantiles(ImmutableSet.of(90, 99), 100, dataset(i)).get(90);
90     }
91     return dummy;
92   }
93 
94   @Benchmark
threePercentiles(int reps)95   double threePercentiles(int reps) {
96     double dummy = 0.0;
97     for (int i = 0; i < reps; i++) {
98       dummy += algorithm.multipleQuantiles(ImmutableSet.of(90, 95, 99), 100, dataset(i)).get(90);
99     }
100     return dummy;
101   }
102 
103   @Benchmark
allDeciles(int reps)104   double allDeciles(int reps) {
105     double dummy = 0.0;
106     for (int i = 0; i < reps; i++) {
107       dummy += algorithm.multipleQuantiles(ALL_DECILE_INDEXES, 10, dataset(i)).get(9);
108     }
109     return dummy;
110   }
111 }
112