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 benchmarks.regression; 18 19 import com.google.caliper.Param; 20 21 /** 22 * Testing the old canard that looping backwards is faster. 23 * 24 * @author Kevin Bourrillion 25 */ 26 public class LoopingBackwardsBenchmark { 27 @Param({"2", "20", "2000", "20000000"}) int max; 28 29 public int timeForwards(int reps) { 30 int dummy = 0; 31 for (int i = 0; i < reps; i++) { 32 for (int j = 0; j < max; j++) { 33 dummy += j; 34 } 35 } 36 return dummy; 37 } 38 39 public int timeBackwards(int reps) { 40 int dummy = 0; 41 for (int i = 0; i < reps; i++) { 42 for (int j = max - 1; j >= 0; j--) { 43 dummy += j; 44 } 45 } 46 return dummy; 47 } 48 } 49