• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package examples;
18 
19 import com.google.caliper.Benchmark;
20 import com.google.caliper.Param;
21 
22 /**
23  * Testing the old canard that looping backwards is faster.
24  */
25 public class LoopingBackwardsBenchmark {
26   @Param({"2", "20", "2000", "20000000"}) int max;
27 
forwards(int reps)28   @Benchmark int forwards(int reps) {
29     int dummy = 0;
30     for (int i = 0; i < reps; i++) {
31       for (int j = 0; j < max; j++) {
32         dummy += j;
33       }
34     }
35     return dummy;
36   }
37 
backwards(int reps)38   @Benchmark int backwards(int reps) {
39     int dummy = 0;
40     for (int i = 0; i < reps; i++) {
41       for (int j = max - 1; j >= 0; j--) {
42         dummy += j;
43       }
44     }
45     return dummy;
46   }
47 }
48