Jesse Wilson | 1440b36 | 2009-12-15 18:54:02 -0800 | [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 com.google.caliper.examples; |
| 18 | |
| 19 | import com.google.caliper.Runner; |
| 20 | import com.google.caliper.SimpleBenchmark; |
| 21 | |
| 22 | /** |
| 23 | * Measures several candidate implementations for mod(). |
| 24 | */ |
| 25 | public class IntModBenchmark extends SimpleBenchmark { |
| 26 | private static final int M = (1 << 16) - 1; |
| 27 | |
| 28 | public int timeConditional(int reps) { |
| 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 | |
| 41 | public int timeDoubleRemainder(int reps) { |
| 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 | private static int doubleRemainderMod(int a, int m) { |
| 50 | return (int) (((a % m) + (long) m) % m); |
| 51 | } |
| 52 | |
| 53 | public int timeRightShiftingMod(int reps) { |
| 54 | int dummy = 5; |
| 55 | for (int i = 0; i < reps; i++) { |
| 56 | dummy += Integer.MAX_VALUE + rightShiftingMod(dummy, M); |
| 57 | } |
| 58 | return dummy; |
| 59 | } |
| 60 | |
| 61 | private static int rightShiftingMod(int a, int m) { |
| 62 | long r = a % m; |
| 63 | return (int) (r + ((r >> 63) & m)); |
| 64 | } |
| 65 | |
| 66 | public int timeLeftShiftingMod(int reps) { |
| 67 | int dummy = 5; |
| 68 | for (int i = 0; i < reps; i++) { |
| 69 | dummy += Integer.MAX_VALUE + leftShiftingMod(dummy, M); |
| 70 | } |
| 71 | return dummy; |
| 72 | } |
| 73 | |
| 74 | private static int leftShiftingMod(int a, int m) { |
| 75 | return (int) ((a + (((long) m) << 32)) % m); |
| 76 | } |
| 77 | |
| 78 | public int timeWrongMod(int reps) { |
| 79 | int dummy = 5; |
| 80 | for (int i = 0; i < reps; i++) { |
| 81 | dummy += Integer.MAX_VALUE + dummy % M; |
| 82 | } |
| 83 | return dummy; |
| 84 | } |
| 85 | |
| 86 | // TODO: remove this from all examples when IDE plugins are ready |
| 87 | public static void main(String[] args) throws Exception { |
| 88 | Runner.main(IntModBenchmark.class, args); |
| 89 | } |
| 90 | } |