What is the best way to optimize or "tweak" LINQ expressions?

When building LINQ expressions (for me, linq for objects), there are many ways to achieve something, much, much better and more efficient than others.

  • Is there a good way to “tweak” or optimize these expressions?
  • What fundamental indicators are used by people and how to collect them?
  • Is there a way to get the “total number of iterations” or some other metric where you could “know” which lower means better?

EDIT

Thanks to Richard / John for your answers.

It seems that what I really want is a way to get a simple "OCount" operation counter for LINQ expression, although I'm not sure if there are hooks in LINQ to allow this. Suppose I have a performance target for a specific hardware equipment (SLA). Ideally, I would add a unit test to confirm that typical data moved through this request will be processed during this allotted time (from the SLA). The problem is that this will run on the build server / developer / etc. which probably looks a little like SLA hardware. Therefore, the idea is that I would define an acceptable maximum "OCount" for the expression, knowing that if the OCount is less than X, it will certainly provide acceptable performance in the SLA on the target "typical" equipment.If OCount exceeds this threshold, the / unit test assembly will generate a warning. Ideally, I would like to have something like this (pseudocode-ish):

var results = [big linq expression run against test dataset];
Assert.IsLess(MAXALLOWABLE_OCOUNT, results.OCount)

results.OCount (n), .

?

, LINQ , / . - , , .

- . - , OCount , -.

# 2 . , Cameron MacFarland , , . , , , unit test, , .

unit test, LINQ, , .

Cameron

+3
3

. , , , .

( - LINQ .)

, , , :

  • = O (n)
  • = O (n)
  • = O ( + + ) (.. , inner + outer, , inner * outer )
  • GroupJoin = , Join, .
  • OrderBy = O (n log n)
  • SelectMany = O (n + )
  • Count = O (1) O (n) , IList
  • Count () = O (n)
  • Max/Min = O (n)
  • All/Any = O (n) ( )
  • Distinct = O (n)
  • / = O (n)
  • SkipWhile/TakeWhile = O (n)

, .

+6
  • SLA ( ), .

  • ( - ).

  • , , , ( , , , , ).

  • , / ( ).

  • 1.

# 3 , LINQ , . , LINQ- , . .

+3

,

, , - , LINQ. , , , LINQ. , LINQ . .

, MoveNext() LINQ

But before returning one item, you need to process each item in the collection.

  • Orderby
  • Except (fully processes another collection)
0
source

Source: https://habr.com/ru/post/1704963/


All Articles