1 /* 2 * Copyright (C) 2011 The Guava Authors 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.common.primitives; 18 19 import com.google.caliper.BeforeExperiment; 20 import com.google.caliper.Benchmark; 21 import java.util.Random; 22 23 /** 24 * Benchmarks for certain methods of {@code UnsignedLongs}. 25 * 26 * @author Eamonn McManus 27 */ 28 public class UnsignedLongsBenchmark { 29 private static final int ARRAY_SIZE = 0x10000; 30 private static final int ARRAY_MASK = 0x0ffff; 31 private static final Random RANDOM_SOURCE = new Random(314159265358979L); 32 private static final long[] longs = new long[ARRAY_SIZE]; 33 private static final long[] divisors = new long[ARRAY_SIZE]; 34 private static final String[] decimalStrings = new String[ARRAY_SIZE]; 35 private static final String[] binaryStrings = new String[ARRAY_SIZE]; 36 private static final String[] hexStrings = new String[ARRAY_SIZE]; 37 private static final String[] prefixedHexStrings = new String[ARRAY_SIZE]; 38 39 @BeforeExperiment setUp()40 void setUp() { 41 for (int i = 0; i < ARRAY_SIZE; i++) { 42 longs[i] = random(); 43 divisors[i] = randomDivisor(longs[i]); 44 decimalStrings[i] = UnsignedLongs.toString(longs[i]); 45 binaryStrings[i] = UnsignedLongs.toString(longs[i], 2); 46 hexStrings[i] = UnsignedLongs.toString(longs[i], 16); 47 prefixedHexStrings[i] = "0x" + hexStrings[i]; 48 } 49 } 50 51 @Benchmark divide(int reps)52 long divide(int reps) { 53 long tmp = 0; 54 for (int i = 0; i < reps; i++) { 55 int j = i & ARRAY_MASK; 56 tmp += UnsignedLongs.divide(longs[j], divisors[j]); 57 } 58 return tmp; 59 } 60 61 @Benchmark remainder(int reps)62 long remainder(int reps) { 63 long tmp = 0; 64 for (int i = 0; i < reps; i++) { 65 int j = i & ARRAY_MASK; 66 tmp += UnsignedLongs.remainder(longs[j], divisors[j]); 67 } 68 return tmp; 69 } 70 71 @Benchmark parseUnsignedLong(int reps)72 long parseUnsignedLong(int reps) { 73 long tmp = 0; 74 // Given that we make three calls per pass, we scale reps down in order 75 // to do a comparable amount of work to other measurements. 76 int scaledReps = reps / 3 + 1; 77 for (int i = 0; i < scaledReps; i++) { 78 int j = i & ARRAY_MASK; 79 tmp += UnsignedLongs.parseUnsignedLong(decimalStrings[j]); 80 tmp += UnsignedLongs.parseUnsignedLong(hexStrings[j], 16); 81 tmp += UnsignedLongs.parseUnsignedLong(binaryStrings[j], 2); 82 } 83 return tmp; 84 } 85 86 @Benchmark parseDecode10(int reps)87 long parseDecode10(int reps) { 88 long tmp = 0; 89 for (int i = 0; i < reps; i++) { 90 int j = i & ARRAY_MASK; 91 tmp += UnsignedLongs.decode(decimalStrings[j]); 92 } 93 return tmp; 94 } 95 96 @Benchmark parseDecode16(int reps)97 long parseDecode16(int reps) { 98 long tmp = 0; 99 for (int i = 0; i < reps; i++) { 100 int j = i & ARRAY_MASK; 101 tmp += UnsignedLongs.decode(prefixedHexStrings[j]); 102 } 103 return tmp; 104 } 105 106 @Benchmark toString(int reps)107 int toString(int reps) { 108 int tmp = 0; 109 // Given that we make three calls per pass, we scale reps down in order 110 // to do a comparable amount of work to other measurements. 111 int scaledReps = reps / 3 + 1; 112 for (int i = 0; i < scaledReps; i++) { 113 int j = i & ARRAY_MASK; 114 long x = longs[j]; 115 tmp += UnsignedLongs.toString(x).length(); 116 tmp += UnsignedLongs.toString(x, 16).length(); 117 tmp += UnsignedLongs.toString(x, 2).length(); 118 } 119 return tmp; 120 } 121 random()122 private static long random() { 123 return RANDOM_SOURCE.nextLong(); 124 } 125 126 // A random value that cannot be 0 and that is unsigned-less-than or equal 127 // to the given dividend, so that we don't have half of our divisions being 128 // trivial because the divisor is bigger than the dividend. 129 // Using remainder here does not give us a uniform distribution but it should 130 // not have a big impact on the measurement. randomDivisor(long dividend)131 private static long randomDivisor(long dividend) { 132 long r = RANDOM_SOURCE.nextLong(); 133 if (dividend == -1) { 134 return r; 135 } else { 136 return UnsignedLongs.remainder(r, dividend + 1); 137 } 138 } 139 } 140