Comment #1 on issue 572 by mcculls: Performance problem or wrong usage?
http://code.google.com/p/google-guice/issues/detail?id=572
Please read: http://www.ibm.com/developerworks/java/library/j-jtp02225.html
It's hard to write a good benchmark - for example in your code the Person
object is never used, and because the simple PersonFactory is a private
static class with a one-line method that just calls new the JIT could be
unrolling that method. It would then see that there's no side-effect and so
the call can safely be removed (ie. dead-code optimization) making it a
very fast loop. The Guice call however involves more method calls making it
unlikely the JIT would inline it or decide that the call could be omitted.
Other issues with your code include no JIT warmup, no guards against GC
disturbing the timing, and using coarse millisecond timing.
Talking about multiples is also misleading: say 'new' took 1ns and guice
took 50ns then while that's still a multiple of 50, it might not lead to a
noticeable slowdown in your application - it would all depend on how many
objects you were creating via Guice.
Obviously Guice will always be slower than pure "new" when constructing
objects, because that's only a few opcodes compared to all the reflection
and management required to analyze the class, find its dependencies, and
inject them. The question is whether this makes a noticeable difference for
a sizeable application. Even then it's not a straight comparison because
while manual wiring may be fast, it's also very fragile and fixed - whereas
good dependency injection is flexible and configurable. Another point to
consider is that injection is typically an up-front cost: once your
application is wired together it will perform exactly the same as if you
used manual wiring.
So to sum up: don't worry about micro-benchmarks, much better to test
against real-world applications.
--
You received this message because you are subscribed to the Google Groups
"google-guice-dev" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-guice-dev?hl=en.