public
Created

  • Download Gist
NullCheck.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
package com.google.common.collect;
 
import com.google.caliper.Runner;
import com.google.caliper.SimpleBenchmark;
 
import java.util.Random;
 
public class NullCheck extends SimpleBenchmark {
Object[] array = new Object[0x10000];
 
@Override
protected void setUp() throws Exception {
Random rng = new Random();
for (int i = 0; i < 0x10000; i++) {
array[i] = rng.nextBoolean() ? new Object() : null;
}
}
 
public int timeIfCheck(int reps) {
int tmp = 0;
for (int i = 0; i < reps; i++) {
Object o = array[i & 0xffff];
if (o != null) {
tmp += o.hashCode();
}
}
return tmp;
}
 
public int timeTryCatch(int reps) {
int tmp = 0;
for (int i = 0; i < reps; i++) {
try {
tmp += array[i & 0xffff].hashCode();
} catch (NullPointerException e) {
}
}
return tmp;
}
 
public static void main(String[] args) {
Runner.main(NullCheck.class, args);
}
 
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.