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