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