diff options
Diffstat (limited to 'guava-tests/benchmark/com/google/common/primitives')
-rw-r--r-- | guava-tests/benchmark/com/google/common/primitives/UnsignedBytesBenchmark.java | 106 | ||||
-rw-r--r-- | guava-tests/benchmark/com/google/common/primitives/UnsignedLongsBenchmark.java | 138 |
2 files changed, 0 insertions, 244 deletions
diff --git a/guava-tests/benchmark/com/google/common/primitives/UnsignedBytesBenchmark.java b/guava-tests/benchmark/com/google/common/primitives/UnsignedBytesBenchmark.java deleted file mode 100644 index eb874f6..0000000 --- a/guava-tests/benchmark/com/google/common/primitives/UnsignedBytesBenchmark.java +++ /dev/null @@ -1,106 +0,0 @@ -/* - * Copyright (C) 2010 The Guava Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.google.common.primitives; - -import com.google.caliper.Param; -import com.google.caliper.Runner; -import com.google.caliper.SimpleBenchmark; - -import java.util.Arrays; -import java.util.Comparator; -import java.util.Random; - -/** - * Microbenchmark for {@link UnsignedBytes}. - * - * @author Hiroshi Yamauchi - */ -public class UnsignedBytesBenchmark extends SimpleBenchmark { - - private byte[] ba1; - private byte[] ba2; - private byte[] ba3; - private byte[] ba4; - private Comparator<byte[]> javaImpl; - private Comparator<byte[]> unsafeImpl; - - // 4, 8, 64, 1K, 1M, 1M (unaligned), 64M, 64M (unaligned) - //@Param({"4", "8", "64", "1024", "1048576", "1048577", "6710884", "6710883"}) - @Param({"4", "8", "64", "1024" }) - private int length; - - @Override - protected void setUp() throws Exception { - Random r = new Random(); - ba1 = new byte[length]; - r.nextBytes(ba1); - ba2 = Arrays.copyOf(ba1, ba1.length); - // Differ at the last element - ba3 = Arrays.copyOf(ba1, ba1.length); - ba4 = Arrays.copyOf(ba1, ba1.length); - ba3[ba1.length - 1] = (byte) 43; - ba4[ba1.length - 1] = (byte) 42; - - javaImpl = UnsignedBytes.lexicographicalComparatorJavaImpl(); - unsafeImpl = - UnsignedBytes.LexicographicalComparatorHolder.UnsafeComparator.INSTANCE; - } - - public void timeLongEqualJava(int reps) { - for (int i = 0; i < reps; ++i) { - if (javaImpl.compare(ba1, ba2) != 0) { - throw new Error(); // deoptimization - } - } - } - - public void timeLongEqualUnsafe(int reps) { - for (int i = 0; i < reps; ++i) { - if (unsafeImpl.compare(ba1, ba2) != 0) { - throw new Error(); // deoptimization - } - } - } - - public void timeDiffLastJava(int reps) { - for (int i = 0; i < reps; ++i) { - if (javaImpl.compare(ba3, ba4) == 0) { - throw new Error(); // deoptimization - } - } - } - - public void timeDiffLastUnsafe(int reps) { - for (int i = 0; i < reps; ++i) { - if (unsafeImpl.compare(ba3, ba4) == 0) { - throw new Error(); // deoptimization - } - } - } - - public static void main(String[] args) { - /* - try { - UnsignedBytesBenchmark bench = new UnsignedBytesBenchmark(); - bench.length = 1024; - bench.setUp(); - bench.timeUnsafe(100000); - } catch (Exception e) { - }*/ - Runner.main(UnsignedBytesBenchmark.class, args); - } -} diff --git a/guava-tests/benchmark/com/google/common/primitives/UnsignedLongsBenchmark.java b/guava-tests/benchmark/com/google/common/primitives/UnsignedLongsBenchmark.java deleted file mode 100644 index 0820bc0..0000000 --- a/guava-tests/benchmark/com/google/common/primitives/UnsignedLongsBenchmark.java +++ /dev/null @@ -1,138 +0,0 @@ -/* - * Copyright (C) 2011 The Guava Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.google.common.primitives; - -import com.google.caliper.Runner; -import com.google.caliper.SimpleBenchmark; - -import java.util.Random; - -/** - * Benchmarks for certain methods of {@code UnsignedLongs}. - * - * @author Eamonn McManus - */ -public class UnsignedLongsBenchmark extends SimpleBenchmark { - private static final int ARRAY_SIZE = 0x10000; - private static final int ARRAY_MASK = 0x0ffff; - private static final Random RANDOM_SOURCE = new Random(314159265358979L); - private static final long[] longs = new long[ARRAY_SIZE]; - private static final long[] divisors = new long[ARRAY_SIZE]; - private static final String[] decimalStrings = new String[ARRAY_SIZE]; - private static final String[] binaryStrings = new String[ARRAY_SIZE]; - private static final String[] hexStrings = new String[ARRAY_SIZE]; - private static final String[] prefixedHexStrings = new String[ARRAY_SIZE]; - - @Override - protected void setUp() { - for (int i = 0; i < ARRAY_SIZE; i++) { - longs[i] = random(); - divisors[i] = randomDivisor(longs[i]); - decimalStrings[i] = UnsignedLongs.toString(longs[i]); - binaryStrings[i] = UnsignedLongs.toString(longs[i], 2); - hexStrings[i] = UnsignedLongs.toString(longs[i], 16); - prefixedHexStrings[i] = "0x" + hexStrings[i]; - } - } - - public long timeDivide(int reps) { - long tmp = 0; - for (int i = 0; i < reps; i++) { - int j = i & ARRAY_MASK; - tmp += UnsignedLongs.divide(longs[j], divisors[j]); - } - return tmp; - } - - public long timeRemainder(int reps) { - long tmp = 0; - for (int i = 0; i < reps; i++) { - int j = i & ARRAY_MASK; - tmp += UnsignedLongs.remainder(longs[j], divisors[j]); - } - return tmp; - } - - public long timeParseUnsignedLong(int reps) { - long tmp = 0; - // Given that we make three calls per pass, we scale reps down in order - // to do a comparable amount of work to other measurements. - int scaledReps = reps / 3 + 1; - for (int i = 0; i < scaledReps; i++) { - int j = i & ARRAY_MASK; - tmp += UnsignedLongs.parseUnsignedLong(decimalStrings[j]); - tmp += UnsignedLongs.parseUnsignedLong(hexStrings[j], 16); - tmp += UnsignedLongs.parseUnsignedLong(binaryStrings[j], 2); - } - return tmp; - } - - public long timeParseDecode10(int reps) { - long tmp = 0; - for (int i = 0; i < reps; i++) { - int j = i & ARRAY_MASK; - tmp += UnsignedLongs.decode(decimalStrings[j]); - } - return tmp; - } - - public long timeParseDecode16(int reps) { - long tmp = 0; - for (int i = 0; i < reps; i++) { - int j = i & ARRAY_MASK; - tmp += UnsignedLongs.decode(prefixedHexStrings[j]); - } - return tmp; - } - - public int timeToString(int reps) { - int tmp = 0; - // Given that we make three calls per pass, we scale reps down in order - // to do a comparable amount of work to other measurements. - int scaledReps = reps / 3 + 1; - for (int i = 0; i < scaledReps; i++) { - int j = i & ARRAY_MASK; - long x = longs[j]; - tmp += UnsignedLongs.toString(x).length(); - tmp += UnsignedLongs.toString(x, 16).length(); - tmp += UnsignedLongs.toString(x, 2).length(); - } - return tmp; - } - - private static long random() { - return RANDOM_SOURCE.nextLong(); - } - - // A random value that cannot be 0 and that is unsigned-less-than or equal - // to the given dividend, so that we don't have half of our divisions being - // trivial because the divisor is bigger than the dividend. - // Using remainder here does not give us a uniform distribution but it should - // not have a big impact on the measurement. - private static long randomDivisor(long dividend) { - long r = RANDOM_SOURCE.nextLong(); - if (dividend == -1) { - return r; - } else { - return UnsignedLongs.remainder(r, dividend + 1); - } - } - - public static void main(String[] args) { - Runner.main(UnsignedLongsBenchmark.class, args); - } -} |