1 /*
2  * Copyright (C) 2008 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.base;
18 
19 import com.google.caliper.Benchmark;
20 import java.util.concurrent.TimeUnit;
21 
22 /**
23  * Simple benchmark: create, start, read. This does not currently report the most useful result
24  * because it's ambiguous to what extent the stopwatch benchmark is being affected by GC.
25  *
26  * @author Kevin Bourrillion
27  */
28 public class StopwatchBenchmark {
29   @Benchmark
stopwatch(int reps)30   long stopwatch(int reps) {
31     long total = 0;
32     for (int i = 0; i < reps; i++) {
33       Stopwatch s = Stopwatch.createStarted();
34       // here is where you would do something
35       total += s.elapsed(TimeUnit.NANOSECONDS);
36     }
37     return total;
38   }
39 
40   @Benchmark
manual(int reps)41   long manual(int reps) {
42     long total = 0;
43     for (int i = 0; i < reps; i++) {
44       long start = System.nanoTime();
45       // here is where you would do something
46       total += (System.nanoTime() - start);
47     }
48     return total;
49   }
50 }
51