Are lambda functions faster than delegates/anonymous functions?

前端 未结 2 1665
情歌与酒
情歌与酒 2021-02-14 01:12

I assumed lambda functions, delegates and anonymous functions with the same body would have the same \"speed\", however, running the follo

2条回答
  •  庸人自扰
    2021-02-14 01:32

    Other peoples results suggest that the performance is the same:

    http://blogs.microsoft.co.il/blogs/alex_golesh/archive/2007/12/11/anonymous-delegates-vs-lambda-expressions-vs-function-calls-performance.aspx

    As noted in the comments, micro-benchmarks are often misleading. There are too many factors over which you have no control, JIT optimisation, garbage collection cycles, etc ...

    See this related question:

    When not to use lambda expressions

    Finally, I think your test is fundamentally flawed! You use a Linq Where extension method to execute your code. However, Linq uses lazy-evaluation, your code will only be executed if you start iterating over the results!

提交回复
热议问题