Skip to content

Instantly share code, notes, and snippets.

@hazam
Created November 11, 2014 15:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hazam/9a0b582d6fa163286267 to your computer and use it in GitHub Desktop.
Save hazam/9a0b582d6fa163286267 to your computer and use it in GitHub Desktop.
Dishomogeneous lists: cast vs wrapperclass
public class PerfTest extends TestCase {
private static final int RUNS = 1000000;
private ArrayList<Object> dishomo = new ArrayList<Object>();
{
dishomo.add(Integer.valueOf(3));
dishomo.add(Double.valueOf(3.0d));
}
private ArrayList<ThisOrThat> thisOrThats = new ArrayList<ThisOrThat>();
public static class ThisOrThat {
Integer theInteger;
Double theDouble;
ThisOrThat(Integer i, Double d) {
this.theInteger = i;
this.theDouble = d;
}
}
{
thisOrThats.add(new ThisOrThat(3, null));
thisOrThats.add(new ThisOrThat(null, 3.0d));
}
public void testCasts() {
long start = System.nanoTime();
for (int i = 0; i < RUNS; i++) {
Object o = dishomo.get(i % 2);
if (o instanceof Integer) {
emptyMethod();
} else {
emptyMethod();
}
}
long time = System.nanoTime() - start;
Log.i("PERF", "testCasts " + time);
}
private void emptyMethod() {
}
public void testNulls() {
long start = System.nanoTime();
for (int i = 0; i < RUNS; i++) {
ThisOrThat tot = thisOrThats.get(i % 2);
if (tot.theInteger == null) {
emptyMethod();
} else {
emptyMethod();
}
}
long time = System.nanoTime() - start;
Log.i("PERF", "testNulls " + time);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment