1 /* 2 * Copyright (C) 2012 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.hash; 18 19 import com.google.caliper.BeforeExperiment; 20 import com.google.caliper.Benchmark; 21 import com.google.caliper.Param; 22 import java.util.Random; 23 import java.util.zip.Adler32; 24 import java.util.zip.CRC32; 25 import java.util.zip.Checksum; 26 27 /** 28 * Benchmarks for comparing {@link Checksum}s and {@link HashFunction}s that wrap {@link Checksum}s. 29 * 30 * <p>Parameters for the benchmark are: 31 * 32 * <ul> 33 * <li>size: The length of the byte array to hash. 34 * </ul> 35 * 36 * @author Colin Decker 37 */ 38 public class ChecksumBenchmark { 39 40 // Use a constant seed for all of the benchmarks to ensure apples to apples comparisons. 41 private static final int RANDOM_SEED = new Random().nextInt(); 42 43 @Param({"10", "1000", "100000", "1000000"}) 44 private int size; 45 46 private byte[] testBytes; 47 48 @BeforeExperiment setUp()49 void setUp() { 50 testBytes = new byte[size]; 51 new Random(RANDOM_SEED).nextBytes(testBytes); 52 } 53 54 // CRC32 55 56 @Benchmark crc32HashFunction(int reps)57 byte crc32HashFunction(int reps) { 58 return runHashFunction(reps, Hashing.crc32()); 59 } 60 61 @Benchmark crc32Checksum(int reps)62 byte crc32Checksum(int reps) throws Exception { 63 byte result = 0x01; 64 for (int i = 0; i < reps; i++) { 65 CRC32 checksum = new CRC32(); 66 checksum.update(testBytes); 67 result = (byte) (result ^ checksum.getValue()); 68 } 69 return result; 70 } 71 72 // Adler32 73 74 @Benchmark adler32HashFunction(int reps)75 byte adler32HashFunction(int reps) { 76 return runHashFunction(reps, Hashing.adler32()); 77 } 78 79 @Benchmark adler32Checksum(int reps)80 byte adler32Checksum(int reps) throws Exception { 81 byte result = 0x01; 82 for (int i = 0; i < reps; i++) { 83 Adler32 checksum = new Adler32(); 84 checksum.update(testBytes); 85 result = (byte) (result ^ checksum.getValue()); 86 } 87 return result; 88 } 89 90 // Helpers + main 91 runHashFunction(int reps, HashFunction hashFunction)92 private byte runHashFunction(int reps, HashFunction hashFunction) { 93 byte result = 0x01; 94 // Trick the JVM to prevent it from using the hash function non-polymorphically 95 result ^= Hashing.crc32().hashInt(reps).asBytes()[0]; 96 result ^= Hashing.adler32().hashInt(reps).asBytes()[0]; 97 for (int i = 0; i < reps; i++) { 98 result ^= hashFunction.hashBytes(testBytes).asBytes()[0]; 99 } 100 return result; 101 } 102 } 103