• 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.security.MessageDigest;
23 
24 /**
25  * Benchmarks for comparing instance creation of {@link MessageDigest}s.
26  *
27  * @author Kurt Alfred Kluever
28  */
29 public class MessageDigestCreationBenchmark {
30 
31   @Param({"MD5", "SHA-1", "SHA-256", "SHA-384", "SHA-512"})
32   private String algorithm;
33 
34   private MessageDigest md;
35 
36   @BeforeExperiment
setUp()37   void setUp() throws Exception {
38     md = MessageDigest.getInstance(algorithm);
39   }
40 
41   @Benchmark
getInstance(int reps)42   int getInstance(int reps) throws Exception {
43     int retValue = 0;
44     for (int i = 0; i < reps; i++) {
45       retValue ^= MessageDigest.getInstance(algorithm).getDigestLength();
46     }
47     return retValue;
48   }
49 
50   @Benchmark
clone(int reps)51   int clone(int reps) throws Exception {
52     int retValue = 0;
53     for (int i = 0; i < reps; i++) {
54       retValue ^= ((MessageDigest) md.clone()).getDigestLength();
55     }
56     return retValue;
57   }
58 }
59