I was using newInstance()
in a sort-of performance-critical area of my code.
The method signature is:
<T extends SomethingElse> T create(Class<T> clasz)
I pass Something.class
as argument, I get an instance of SomethingElse
, created with newInstance()
.
Today I got back to clear this performance TODO from the list, so I ran a couple of tests of new
operator versus newInstance()
. I was very surprised with the performance penalty of newInstance()
.
I wrote a little about it, here: http://bruno.linker45.eu/2010/07/20/new-vs-newinstance/
(Sorry about the self promotion... I'd place the text here, but this question would grow out of proportions.)
What I'd love to know is why does the -server
flag provide such a performance boost when the number of objects being created grows largely and not for "low" values, say, 100 or 1000.
I did learn my lesson with the whole reflections thing, this is just curiosity about the optimisations the JVM performs in runtime, especially with the -server
flag. Also, if I'm doing something wrong in the test, I'd appreciate your feedback!
Edit: I've added a warmup phase and the results are now more stable. Thanks for the input!