RandomBenchmark.java revision 5a7833b406bb2716b057d3ed923f22f1f86b2a20
1/*
2 * Copyright (C) 2010 Google Inc.
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
17package benchmarks.regression;
18
19import java.security.SecureRandom;
20import java.util.Random;
21import com.google.caliper.Param;
22import com.google.caliper.SimpleBenchmark;
23
24public class RandomBenchmark extends SimpleBenchmark {
25    public void timeNewRandom(int reps) throws Exception {
26        for (int i = 0; i < reps; ++i) {
27            Random rng = new Random();
28            rng.nextInt();
29        }
30    }
31
32    public void timeReusedRandom(int reps) throws Exception {
33        Random rng = new Random();
34        for (int i = 0; i < reps; ++i) {
35            rng.nextInt();
36        }
37    }
38
39    public void timeReusedSecureRandom(int reps) throws Exception {
40        SecureRandom rng = new SecureRandom();
41        for (int i = 0; i < reps; ++i) {
42            rng.nextInt();
43        }
44    }
45
46    public void timeNewSecureRandom(int reps) throws Exception {
47        for (int i = 0; i < reps; ++i) {
48            SecureRandom rng = new SecureRandom();
49            rng.nextInt();
50        }
51    }
52}
53