Elliott Hughes | 97aba27 | 2013-02-13 16:21:51 -0800 | [diff] [blame] | 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; |
| 18 | |
| 19 | import com.google.caliper.Param; |
| 20 | import com.google.caliper.Runner; |
| 21 | import com.google.caliper.SimpleBenchmark; |
| 22 | |
| 23 | /** |
| 24 | * What do various kinds of addition cost? |
| 25 | */ |
| 26 | public class AdditionBenchmark extends SimpleBenchmark { |
| 27 | public int timeAddConstantToLocalInt(int reps) { |
| 28 | int result = 0; |
| 29 | for (int i = 0; i < reps; ++i) { |
| 30 | result += 123; |
| 31 | } |
| 32 | return result; |
| 33 | } |
| 34 | public int timeAddTwoLocalInts(int reps) { |
| 35 | int result = 0; |
| 36 | int constant = 123; |
| 37 | for (int i = 0; i < reps; ++i) { |
| 38 | result += constant; |
| 39 | } |
| 40 | return result; |
| 41 | } |
| 42 | public long timeAddConstantToLocalLong(int reps) { |
| 43 | long result = 0; |
| 44 | for (int i = 0; i < reps; ++i) { |
| 45 | result += 123L; |
| 46 | } |
| 47 | return result; |
| 48 | } |
| 49 | public long timeAddTwoLocalLongs(int reps) { |
| 50 | long result = 0; |
| 51 | long constant = 123L; |
| 52 | for (int i = 0; i < reps; ++i) { |
| 53 | result += constant; |
| 54 | } |
| 55 | return result; |
| 56 | } |
| 57 | public float timeAddConstantToLocalFloat(int reps) { |
| 58 | float result = 0.0f; |
| 59 | for (int i = 0; i < reps; ++i) { |
| 60 | result += 123.0f; |
| 61 | } |
| 62 | return result; |
| 63 | } |
| 64 | public float timeAddTwoLocalFloats(int reps) { |
| 65 | float result = 0.0f; |
| 66 | float constant = 123.0f; |
| 67 | for (int i = 0; i < reps; ++i) { |
| 68 | result += constant; |
| 69 | } |
| 70 | return result; |
| 71 | } |
| 72 | public double timeAddConstantToLocalDouble(int reps) { |
| 73 | double result = 0.0; |
| 74 | for (int i = 0; i < reps; ++i) { |
| 75 | result += 123.0; |
| 76 | } |
| 77 | return result; |
| 78 | } |
| 79 | public double timeAddTwoLocalDoubles(int reps) { |
| 80 | double result = 0.0; |
| 81 | double constant = 123.0; |
| 82 | for (int i = 0; i < reps; ++i) { |
| 83 | result += constant; |
| 84 | } |
| 85 | return result; |
| 86 | } |
| 87 | } |