Home | History | Annotate | Download | only in primitives
      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.primitives;
     18 
     19 import com.google.caliper.BeforeExperiment;
     20 import com.google.caliper.Benchmark;
     21 import com.google.caliper.Param;
     22 
     23 import java.util.Arrays;
     24 import java.util.Comparator;
     25 import java.util.Random;
     26 
     27 /**
     28  * Microbenchmark for {@link UnsignedBytes}.
     29  *
     30  * @author Hiroshi Yamauchi
     31  */
     32 public class UnsignedBytesBenchmark {
     33 
     34   private byte[] ba1;
     35   private byte[] ba2;
     36   private byte[] ba3;
     37   private byte[] ba4;
     38   private Comparator<byte[]> javaImpl;
     39 
     40   // 4, 8, 64, 1K, 1M, 1M (unaligned), 64M, 64M (unaligned)
     41   //@Param({"4", "8", "64", "1024", "1048576", "1048577", "6710884", "6710883"})
     42   @Param({"4", "8", "64", "1024" })
     43   private int length;
     44 
     45   @BeforeExperiment
     46   void setUp() throws Exception {
     47     Random r = new Random();
     48     ba1 = new byte[length];
     49     r.nextBytes(ba1);
     50     ba2 = Arrays.copyOf(ba1, ba1.length);
     51     // Differ at the last element
     52     ba3 = Arrays.copyOf(ba1, ba1.length);
     53     ba4 = Arrays.copyOf(ba1, ba1.length);
     54     ba3[ba1.length - 1] = (byte) 43;
     55     ba4[ba1.length - 1] = (byte) 42;
     56 
     57     javaImpl = UnsignedBytes.lexicographicalComparatorJavaImpl();
     58   }
     59 
     60   @Benchmark void longEqualJava(int reps) {
     61     for (int i = 0; i < reps; ++i) {
     62       if (javaImpl.compare(ba1, ba2) != 0) {
     63         throw new Error(); // deoptimization
     64       }
     65     }
     66   }
     67 
     68   @Benchmark void diffLastJava(int reps) {
     69     for (int i = 0; i < reps; ++i) {
     70       if (javaImpl.compare(ba3, ba4) == 0) {
     71         throw new Error(); // deoptimization
     72       }
     73     }
     74   }
     75 
     76   /*
     77   try {
     78     UnsignedBytesBenchmark bench = new UnsignedBytesBenchmark();
     79     bench.length = 1024;
     80     bench.setUp();
     81     bench.timeUnsafe(100000);
     82   } catch (Exception e) {
     83   }*/
     84 }
     85