On May 20, 2009, at 3:38 PM, CuppoJava wrote:
I'm using "apply" a lot in my code, and a small micro-benchmark is telling me that it's a very slow operation. Can someone confirm this? I'm wondering whether my benchmark is incorrect, or whether there's a hidden reflection that I'm not seeing.
"apply" supports a lot of flexibility in calling that makes it a very useful tool. It's much slower than a direct function call, but it "does a lot more" in both senses of the phrase:
- it offers flexibility that enables succinct, readable code - it does more work at runtimeI ran your examples through YourKit and asked for a Tracing CPU profile. (I had to reduce the iteration count to 10,000 to make the instrumented runtime reasonable on my machine.)
Here's are the methods that take up most of the CPU time ("hot spots") for the direct call case:
http://tinyurl.com/direct-case And the "apply" case: http://tinyurl.com/apply-caseConsidering "hot spot" method call count as an indicator of work done, for 10,000 iterations the direct case generates about 40,000 method calls, while the apply case generates about 470,000 method calls, a factor of almost 12.
The usual microbenchmark caveats apply here. These results are not necessarily a strong indicator of how using apply will impact a real application's runtime.
If profiling an application shows that the speed of apply is a problem, it may be worth coding around it. In general, I consider it a useful tool that earns its keep.
--Steve
smime.p7s
Description: S/MIME cryptographic signature