Skip to content

Instantly share code, notes, and snippets.

@svenefftinge
Created December 21, 2011 14:49
Show Gist options
  • Save svenefftinge/1506285 to your computer and use it in GitHub Desktop.
Save svenefftinge/1506285 to your computer and use it in GitHub Desktop.
This one prints : "Took : 31 ms, number of elements : 3000000"
public static void main(String[] args) {
int iterations = 1000000;
List<?> items = newArrayList("foo", 23, true);
List<Object> absoluteResult = new ArrayList<Object>(iterations*items.size());
long before = System.currentTimeMillis();
for (int i=0; i < iterations; i++) {
for (int j=0 ; j< items.size(); j++) {
absoluteResult.add(foo(items.get(j)));
}
}
System.out.println("Took : "+(System.currentTimeMillis() - before)
+" ms, number of elements : "+absoluteResult.size());
}
static String foo(Object s) {
if (s instanceof String) {
return "String";
} else if (s instanceof Boolean) {
return "Boolean";
} else if (s instanceof Integer) {
return "Integer";
} else {
throw new IllegalArgumentException();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment