• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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, 0, testBytes.length);
67       result = (byte) (result ^ checksum.getValue());
68     }
69     return result;
70   }
71 
72   // CRC32C
73 
74   @Benchmark
crc32cHashFunction(int reps)75   byte crc32cHashFunction(int reps) {
76     return runHashFunction(reps, Hashing.crc32c());
77   }
78 
79   // Adler32
80 
81   @Benchmark
adler32HashFunction(int reps)82   byte adler32HashFunction(int reps) {
83     return runHashFunction(reps, Hashing.adler32());
84   }
85 
86   @Benchmark
adler32Checksum(int reps)87   byte adler32Checksum(int reps) throws Exception {
88     byte result = 0x01;
89     for (int i = 0; i < reps; i++) {
90       Adler32 checksum = new Adler32();
91       checksum.update(testBytes, 0, testBytes.length);
92       result = (byte) (result ^ checksum.getValue());
93     }
94     return result;
95   }
96 
97   // Helpers + main
98 
runHashFunction(int reps, HashFunction hashFunction)99   private byte runHashFunction(int reps, HashFunction hashFunction) {
100     byte result = 0x01;
101     // Trick the JVM to prevent it from using the hash function non-polymorphically
102     result ^= Hashing.crc32().hashInt(reps).asBytes()[0];
103     result ^= Hashing.adler32().hashInt(reps).asBytes()[0];
104     for (int i = 0; i < reps; i++) {
105       result ^= hashFunction.hashBytes(testBytes).asBytes()[0];
106     }
107     return result;
108   }
109 }
110