Skip to content

Calling Action twice as expensive as method. Why? #87400

Answered by jakobbotsch
maxima120 asked this question in General
Discussion options

You must be logged in to vote

Not quite, I mean if the benchmark is changed to

        public double ActionRunner()
        {
            double res = 0;

            var action = Action;
            for (int i = 0; i < data.Length - 1; i++)
            {
                res += action(data[i], data[i + 1]);
            }

            return res;
        }

In that case it should be on par with the NoMethodRunner and MethodRunner benchmarks -- the JIT is then able to check once before the loop that action 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…

Replies: 4 comments 6 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
6 replies
@jakobbotsch
Comment options

@maxima120
Comment options

@jakobbotsch
Comment options

Answer selected by danmoseley
@maxima120
Comment options

@jakobbotsch
Comment options

@maxima120
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
5 participants