Paul Duffin | 7fc0b45 | 2015-11-10 17:45:15 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 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 | |
Paul Duffin | e236301 | 2015-11-30 16:20:41 +0000 | [diff] [blame^] | 19 | import com.google.caliper.Benchmark; |
Paul Duffin | 7fc0b45 | 2015-11-10 17:45:15 +0000 | [diff] [blame] | 20 | |
| 21 | /** |
| 22 | * Measures several candidate implementations for mod(). |
| 23 | */ |
| 24 | @SuppressWarnings("SameParameterValue") |
Paul Duffin | e236301 | 2015-11-30 16:20:41 +0000 | [diff] [blame^] | 25 | public class IntModBenchmark { |
Paul Duffin | 7fc0b45 | 2015-11-10 17:45:15 +0000 | [diff] [blame] | 26 | private static final int M = (1 << 16) - 1; |
| 27 | |
Paul Duffin | e236301 | 2015-11-30 16:20:41 +0000 | [diff] [blame^] | 28 | @Benchmark int conditional(int reps) { |
Paul Duffin | 7fc0b45 | 2015-11-10 17:45:15 +0000 | [diff] [blame] | 29 | int dummy = 5; |
| 30 | for (int i = 0; i < reps; i++) { |
| 31 | dummy += Integer.MAX_VALUE + conditionalMod(dummy, M); |
| 32 | } |
| 33 | return dummy; |
| 34 | } |
| 35 | |
| 36 | private static int conditionalMod(int a, int m) { |
| 37 | int r = a % m; |
| 38 | return r < 0 ? r + m : r; |
| 39 | } |
| 40 | |
Paul Duffin | e236301 | 2015-11-30 16:20:41 +0000 | [diff] [blame^] | 41 | @Benchmark int doubleRemainder(int reps) { |
Paul Duffin | 7fc0b45 | 2015-11-10 17:45:15 +0000 | [diff] [blame] | 42 | int dummy = 5; |
| 43 | for (int i = 0; i < reps; i++) { |
| 44 | dummy += Integer.MAX_VALUE + doubleRemainderMod(dummy, M); |
| 45 | } |
| 46 | return dummy; |
| 47 | } |
| 48 | |
| 49 | @SuppressWarnings("NumericCastThatLosesPrecision") // result of % by an int must be in int range |
| 50 | private static int doubleRemainderMod(int a, int m) { |
| 51 | return (int) ((a % m + (long) m) % m); |
| 52 | } |
| 53 | |
Paul Duffin | e236301 | 2015-11-30 16:20:41 +0000 | [diff] [blame^] | 54 | @Benchmark int rightShiftingMod(int reps) { |
Paul Duffin | 7fc0b45 | 2015-11-10 17:45:15 +0000 | [diff] [blame] | 55 | int dummy = 5; |
| 56 | for (int i = 0; i < reps; i++) { |
| 57 | dummy += Integer.MAX_VALUE + rightShiftingMod(dummy, M); |
| 58 | } |
| 59 | return dummy; |
| 60 | } |
| 61 | |
| 62 | @SuppressWarnings("NumericCastThatLosesPrecision") // must be in int range |
| 63 | private static int rightShiftingMod(int a, int m) { |
| 64 | long r = a % m; |
| 65 | return (int) (r + (r >> 63 & m)); |
| 66 | } |
| 67 | |
Paul Duffin | e236301 | 2015-11-30 16:20:41 +0000 | [diff] [blame^] | 68 | @Benchmark int leftShiftingMod(int reps) { |
Paul Duffin | 7fc0b45 | 2015-11-10 17:45:15 +0000 | [diff] [blame] | 69 | int dummy = 5; |
| 70 | for (int i = 0; i < reps; i++) { |
| 71 | dummy += Integer.MAX_VALUE + leftShiftingMod(dummy, M); |
| 72 | } |
| 73 | return dummy; |
| 74 | } |
| 75 | |
| 76 | @SuppressWarnings("NumericCastThatLosesPrecision") // result of % by an int must be in int range |
| 77 | private static int leftShiftingMod(int a, int m) { |
| 78 | return (int) ((a + ((long) m << 32)) % m); |
| 79 | } |
| 80 | |
Paul Duffin | e236301 | 2015-11-30 16:20:41 +0000 | [diff] [blame^] | 81 | @Benchmark int wrongMod(int reps) { |
Paul Duffin | 7fc0b45 | 2015-11-10 17:45:15 +0000 | [diff] [blame] | 82 | int dummy = 5; |
| 83 | for (int i = 0; i < reps; i++) { |
| 84 | dummy += Integer.MAX_VALUE + dummy % M; |
| 85 | } |
| 86 | return dummy; |
| 87 | } |
Paul Duffin | e236301 | 2015-11-30 16:20:41 +0000 | [diff] [blame^] | 88 | } |