Calling Action twice as expensive as method. Why? #87400
-
I am trying to compare performance of different ways to execute code. The rationale behind is that I need to find best way to propagate changes from a server using an API to WPF. This is high-throughput, near real-time app. The API in question is being built and I have some freedom to influence its design.
|
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 6 replies
-
For benchmarks please use something like https://benchmarkdotnet.org that accounts for JIT-compilation, warm-up, etc. The results are much more precise than measuring the way you have shown. In you setup the "method" is simple, and will very likely be inlined by the JIT. Whereas the "action" (actually a Func-delegate) won't be inlined (or at least not inlined in the same sense as the "method" will be). So the comparison is not really valid. If the code in method is larger or if you put
Did you think of using SignalR for such cases? |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
It is expected that an indirect call is more expensive than a direct call. |
Beta Was this translation helpful? Give feedback.
-
PGO (now enabled by default in 8 Preview 5) will diminish the cost of the
For more realistic tests I would expect it to do pretty well. @jakobbotsch can you look into why we don't clone the loop in |
Beta Was this translation helpful? Give feedback.
Not quite, I mean if the benchmark is changed to
In that case it should be on par with the
NoMethodRunner
andMethodRunner
benchmarks -- the JIT is then able to check once before the loop thataction
is pointing to the(d1, d2) => d1 + d2
lambda and effectively generate the same loop as in the cases without a lambda.That might not be too relevant for your actual use case, assuming the event/action is not invoked i…