blob: bed415f2eda1c72ec5e31539d5c178a56f6ad5a2 [file] [log] [blame]
Paul Duffin7fc0b452015-11-10 17:45:15 +00001/*
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
17package examples;
18
Paul Duffine2363012015-11-30 16:20:41 +000019import com.google.caliper.Benchmark;
Paul Duffin7fc0b452015-11-10 17:45:15 +000020
21/**
22 * Measures several candidate implementations for mod().
23 */
24@SuppressWarnings("SameParameterValue")
Paul Duffine2363012015-11-30 16:20:41 +000025public class IntModBenchmark {
Paul Duffin7fc0b452015-11-10 17:45:15 +000026 private static final int M = (1 << 16) - 1;
27
Paul Duffine2363012015-11-30 16:20:41 +000028 @Benchmark int conditional(int reps) {
Paul Duffin7fc0b452015-11-10 17:45:15 +000029 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 Duffine2363012015-11-30 16:20:41 +000041 @Benchmark int doubleRemainder(int reps) {
Paul Duffin7fc0b452015-11-10 17:45:15 +000042 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 Duffine2363012015-11-30 16:20:41 +000054 @Benchmark int rightShiftingMod(int reps) {
Paul Duffin7fc0b452015-11-10 17:45:15 +000055 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 Duffine2363012015-11-30 16:20:41 +000068 @Benchmark int leftShiftingMod(int reps) {
Paul Duffin7fc0b452015-11-10 17:45:15 +000069 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 Duffine2363012015-11-30 16:20:41 +000081 @Benchmark int wrongMod(int reps) {
Paul Duffin7fc0b452015-11-10 17:45:15 +000082 int dummy = 5;
83 for (int i = 0; i < reps; i++) {
84 dummy += Integer.MAX_VALUE + dummy % M;
85 }
86 return dummy;
87 }
Paul Duffine2363012015-11-30 16:20:41 +000088}