Created
November 11, 2014 15:16
-
-
Save hazam/9a0b582d6fa163286267 to your computer and use it in GitHub Desktop.
Dishomogeneous lists: cast vs wrapperclass
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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