1 /* 2 * Copyright (C) 2010 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.base; 18 19 import com.google.caliper.Benchmark; 20 21 /** 22 * Some microbenchmarks for the {@link com.google.common.base.Objects} class. 23 * 24 * @author Ben L. Titzer 25 */ 26 public class ObjectsBenchmark { 27 28 private static final Integer I0 = -45; 29 private static final Integer I1 = -1; 30 private static final Integer I2 = 3; 31 private static final String S0 = "3"; 32 private static final String S1 = "Ninety five"; 33 private static final String S2 = "44 one million"; 34 private static final String S3 = "Lowly laundry lefties"; 35 private static final String S4 = "89273487U#*&#"; 36 private static final Double D0 = 9.234d; 37 private static final Double D1 = -1.2e55; 38 39 @Benchmark hashString_2(int reps)40 int hashString_2(int reps) { 41 int dummy = 0; 42 for (int i = 0; i < reps; i++) { 43 dummy += Objects.hashCode(S0, S1); 44 } 45 return dummy; 46 } 47 48 @Benchmark hashString_3(int reps)49 int hashString_3(int reps) { 50 int dummy = 0; 51 for (int i = 0; i < reps; i++) { 52 dummy += Objects.hashCode(S0, S1, S2); 53 } 54 return dummy; 55 } 56 57 @Benchmark hashString_4(int reps)58 int hashString_4(int reps) { 59 int dummy = 0; 60 for (int i = 0; i < reps; i++) { 61 dummy += Objects.hashCode(S0, S1, S2, S3); 62 } 63 return dummy; 64 } 65 66 @Benchmark hashString_5(int reps)67 int hashString_5(int reps) { 68 int dummy = 0; 69 for (int i = 0; i < reps; i++) { 70 dummy += Objects.hashCode(S0, S1, S2, S3, S4); 71 } 72 return dummy; 73 } 74 75 @Benchmark hashMixed_5(int reps)76 int hashMixed_5(int reps) { 77 int dummy = 0; 78 for (int i = 0; i < reps; i++) { 79 dummy += Objects.hashCode(I2, S1, D1, S2, I0); 80 dummy += Objects.hashCode(D0, I1, S3, I2, S0); 81 } 82 return dummy; 83 } 84 } 85