Skip to content

Instantly share code, notes, and snippets.

@a-peyrard
Created February 22, 2015 14:49
Show Gist options
  • Save a-peyrard/8160a7561f2fdb445337 to your computer and use it in GitHub Desktop.
Save a-peyrard/8160a7561f2fdb445337 to your computer and use it in GitHub Desktop.
isAssignableFrom benchmark
package io.restx.benchmarks;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import org.openjdk.jmh.annotations.Benchmark;
import java.io.Serializable;
import java.lang.reflect.Type;
import java.util.AbstractCollection;
import java.util.AbstractList;
import java.util.AbstractMap;
import java.util.AbstractSequentialList;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Deque;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.RandomAccess;
import java.util.TreeMap;
import restx.common.TypeReference;
import restx.common.Types;
/**
* @author apeyrard
*/
public class IsAssignableFromBenchmark {
static final Class<?>[] classesPool = new Class<?>[] {
Serializable.class,
List.class,
Iterable.class,
AbstractList.class,
AbstractCollection.class,
RandomAccess.class,
ArrayList.class,
ImmutableList.class,
LinkedList.class,
Collection.class,
Cloneable.class,
AbstractSequentialList.class,
Deque.class,
Map.class,
HashMap.class,
ImmutableMap.class,
AbstractMap.class,
TreeMap.class
};
static final Type[] typesPool = new Type[] {
Serializable.class,
List.class,
new TypeReference<List<String>>(){}.getType(),
new TypeReference<List<Integer>>(){}.getType(),
Iterable.class,
new TypeReference<Iterable<String>>(){}.getType(),
new TypeReference<Iterable<Integer>>(){}.getType(),
AbstractList.class,
new TypeReference<List<AbstractList<String>>>(){}.getType(),
new TypeReference<List<AbstractList<Integer>>>(){}.getType(),
AbstractCollection.class,
new TypeReference<AbstractCollection<String>>(){}.getType(),
new TypeReference<AbstractCollection<Integer>>(){}.getType(),
RandomAccess.class,
ArrayList.class,
new TypeReference<ArrayList<String>>(){}.getType(),
new TypeReference<ArrayList<Integer>>(){}.getType(),
ImmutableList.class,
new TypeReference<ImmutableList<String>>(){}.getType(),
new TypeReference<ImmutableList<Integer>>(){}.getType(),
LinkedList.class,
new TypeReference<LinkedList<String>>(){}.getType(),
new TypeReference<LinkedList<Integer>>(){}.getType(),
Collection.class,
new TypeReference<Collection<String>>(){}.getType(),
new TypeReference<Collection<Integer>>(){}.getType(),
Cloneable.class,
AbstractSequentialList.class,
new TypeReference<AbstractSequentialList<String>>(){}.getType(),
new TypeReference<AbstractSequentialList<Integer>>(){}.getType(),
Deque.class,
new TypeReference<Deque<String>>(){}.getType(),
new TypeReference<Deque<Integer>>(){}.getType(),
Map.class,
new TypeReference<Map<String, Number>>(){}.getType(),
new TypeReference<Map<Integer, Double>>(){}.getType(),
HashMap.class,
new TypeReference<HashMap<String, Number>>(){}.getType(),
new TypeReference<HashMap<Integer, Double>>(){}.getType(),
ImmutableMap.class,
new TypeReference<ImmutableMap<String, Number>>(){}.getType(),
new TypeReference<ImmutableMap<Integer, Double>>(){}.getType(),
AbstractMap.class,
new TypeReference<AbstractMap<String, Number>>(){}.getType(),
new TypeReference<AbstractMap<Integer, Double>>(){}.getType(),
TreeMap.class,
new TypeReference<TreeMap<String, Number>>(){}.getType(),
new TypeReference<TreeMap<Integer, Double>>(){}.getType()
};
static final Random random = new Random(System.currentTimeMillis());
@Benchmark
public int blank() {
int first = Math.abs(random.nextInt(classesPool.length));
int second = Math.abs(random.nextInt(classesPool.length));
Class<?> firstClass = classesPool[first];
Class<?> secondClass = classesPool[second];
// nothing, just common benchmarks code
return firstClass.hashCode() + secondClass.hashCode();
}
@Benchmark
public boolean classIsAssignableFrom() {
int first = Math.abs(random.nextInt(classesPool.length));
int second = Math.abs(random.nextInt(classesPool.length));
Class<?> firstClass = classesPool[first];
Class<?> secondClass = classesPool[second];
boolean assignableFromFirstToSecond = firstClass.isAssignableFrom(secondClass);
boolean assignableFromSecondToFirst = secondClass.isAssignableFrom(firstClass);
return assignableFromFirstToSecond & assignableFromSecondToFirst;
}
@Benchmark
public boolean typesIsAssignableFrom() {
int first = Math.abs(random.nextInt(classesPool.length));
int second = Math.abs(random.nextInt(classesPool.length));
Class<?> firstClass = classesPool[first];
Class<?> secondClass = classesPool[second];
boolean assignableFromFirstToSecond = Types.isAssignableFrom(firstClass, secondClass);
boolean assignableFromSecondToFirst = Types.isAssignableFrom(secondClass, firstClass);
return assignableFromFirstToSecond & assignableFromSecondToFirst;
}
@Benchmark
public boolean typesWithGenericsIsAssignableFrom() {
int first = Math.abs(random.nextInt(typesPool.length));
int second = Math.abs(random.nextInt(typesPool.length));
Type firstType = typesPool[first];
Type secondType = typesPool[second];
boolean assignableFromFirstToSecond = Types.isAssignableFrom(firstType, secondType);
boolean assignableFromSecondToFirst = Types.isAssignableFrom(secondType, firstType);
return assignableFromFirstToSecond & assignableFromSecondToFirst;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment