Tuesday, March 13, 2007

Checked Generic Collections Microbenchmark

After skim-reading "How'd this String get into my List?", I idly pondered the cost of using the 'checked' collections when using generic collections in Java.

One little microbenchmark later, and it seems, at least in my environment, that a checked collection takes about twice as long to do an add().

By way of example:
1.95M adds: 582ms unchecked, 1300ms checked
0.95M adds: 475ms unchecked, 616ms checked
450K adds: 239ms unchecked, 348ms checked

The results weren't so consistent that I'd want to lay money on it, but it does, at least, imply that the overhead cost of a checked collection is fairly low, with the possible exception of extreme cases where you're trying to find any performance advantage.

If you're exceptionally curious about how the microbenchmark was performed, I'm willing to post code, but as with any microbenchmark, you're better taking it with a grain of salt, and verifying your own scenarios if it's vital for your work.

2 comments:

Anthony Chaves said...

Hi Geoffrey,

I started to post a comment about Generics performance on your blog but it became a little too long so I posted it back at my blog Technical Notes. Thanks for reading mine and commenting on it in the first place!

Anthony

Geoffrey Wiseman said...

In case there's any confusion for readers, I thought I'd clarify the two things I was comparing:
- new ArrayList<String>();
- Collections.checkedList( new ArrayList<String>(), String.class );