What is the Efficiency and Performance of LINQ and Lambda Expression in .Net?

后端 未结 6 1397
粉色の甜心
粉色の甜心 2020-11-27 12:46

I have used .Net 3.5 and VS 2008 for more than a month. Like most .Net developers, I have evolved from years experience in .Net 1.0 & 2.0 and VS 2005. Just recently, I d

相关标签:
6条回答
  • 2020-11-27 13:25

    There is no performance difference between LINQ queries and Lambda expressions.

    You should completely understand how LINQ feature(both Lambda, LINQ queries) works in .Net before you are looking into performance issues.

    Basically you can work with any one of both LINQ queries and Lambda expressions..

    LINQ Queries

    1. It is high level readable query.

    2. It is converted into equalent Lambda expressions and Lambda expressions added as nodes into an expression tree. Expression tree which makes structure of lambda expressions. This is done by compiler.

    3. Query provider looks into expressions(added as nodes in expression tree) and produces equalent SQL query operators thus equalent sql query formed during runtime.

    4. Return Type : Result set (IEnumerable).

    Lambda Expressions

    1. It is a set of expressions/statements and creates delegate / expression tree. It can be passed to a function as an argument.

    2. It supports all the LINQ methods like LINQ queries. (Where,Select,Count,Sum,etc)

    3. An expression tree formed which makes structure of lambda expressions. This is done by compiler.

    4. Query provider looks into expressions(expression tree) and produces equalent SQL query during runtime.

    5. Return Type : Delagate / Expression Tree

    Which is Best?

    You can understand the LINQ (Queries,Lambda) If you look into the above points.

    Advantage of LINQ query - It is readable.

    Advantage of Lambda

    1. Lambda will have an advantage since it creates a delegate and by using the delagte you can just pass the input paremeters and get the result for the different input parameters.You need not write different queries for different criteria as well.

    2. You can create dynamic query by using Lambda expressions and Expression trees.

    3. You can use Lambda expressions if you want to pass the result of statement(s) to a method as an argument.

    4. expressions are shorter.

    So Lambda expression is the best for development over LINQ queries.

    0 讨论(0)
  • 2020-11-27 13:28

    For LINQ queries, with the 'new syntax', the IL (code) generated, is fundamentally no different than calling the extension methods provided by Enumerable and Queryable directly.

    0 讨论(0)
  • 2020-11-27 13:31

    Technically the fastest way is to control all the minutia yourself. Here are some performance tests. Notice that the foreach keyword and the ForEach LINQ construct are identically far far slower than just using for and writing procedural code.

    However, the compiler can and will be improved and you can always profile your code and optimize any problematic areas. It is generally recommended to use the more expressive features that make code easier to read unless you really need the extra nanoseconds.

    0 讨论(0)
  • 2020-11-27 13:36

    Dont optimize prematurely. Use Linq and the new extension methods liberally if they improve readability and profile your application afterwards.

    Most times the difference between Linq and using plain for loops is not relevant at all. The improved maintainability of your code should be worth a few ms. Linq can be slower because it works on enumerators which are implemented as state machines. So plain for(...) loops will be faster.

    I would recommend following Lasse V. Karlsens advice and append http://www.davesquared.net/2009/07/enumerables-linq-and-speed.html to his link list.

    0 讨论(0)
  • 2020-11-27 13:39

    There's no one single answer that will suffice here.

    LINQ has many uses, and many implementations, and thus many implications to the efficiency of your code.

    As with every piece of technology at our fingertips, LINQ can and will be abused and misused alike, and the ability to distinguish between that, and proper usage, is only dependent on one thing: knowledge.

    So the best advice I can give you is to go and read up on how LINQ is really implemented.

    Things you should check into are:

    • LINQ and how it uses the methods and extension methods on existing collection types
      • How LINQ Works
      • How LINQ works internally (Stack Overflow)
      • How does coding with LINQ work? What happens behind the scenes?
    • How LINQ-to-objects and LINQ-to-SQL differs
      • What is the difference between LINQ query expressions and extension methods (Stack Overflow)
    • Alternatives to the new LINQ syntax, for instance, the usage of the .Where(...) extension method for collections

    And as always, when looking at efficiency questions, the only safe approach is just to measure. Create a piece of code using LINQ that does a single, know, thing, and create an alternative, then measure both, and try to improve. Guessing and assuming will only lead to bad results.

    0 讨论(0)
  • 2020-11-27 13:41

    In some cases LINQ is just as fast if not faster than other methods, but in other cases it can be slower. We work on a project that we converted to linq and the data lookup is faster but the merging of data between two tables is much slower. There is a little overhead, but in most cases I don't see the speed difference having much effect on your program.

    0 讨论(0)
提交回复
热议问题