-2

Very often use LINQ to filter object array

I ran a test expression that produces the same result, but as different times, I would like to know the reason for this behavior.

public long testTimeOperetionWHERE()
{
    Object[] list = opCoIn.getList();
    System.Diagnostics.Stopwatch sw = System.Diagnostics.Stopwatch.StartNew();


    int i = 0;
    while (i<20000)
    {
        var result = list.Where(o => o.Id>0)
                     .Where(o => o.Import>0)
                     .Where(o => o.OrderConfirm==o.NumberConfirm)
                     .Where(o => o.IdActiveCustomer>100 );
        i++;
    }

    long e = sw.ElapsedMilliseconds; 

    return e;
}

The time cost result always varies between 90-80

In this case

public long testTimeOperetionAND()
{
    Object[] list = opCoIn.getList();
    System.Diagnostics.Stopwatch sw = System.Diagnostics.Stopwatch.StartNew();


    int i = 0;
    while (i < 20000)
    {
        var result = list.Where(o => o.Id > 0
                                && o.Import > 0
                                && o.OrderConfirm==o.NumberConfirm
                                && o.IdActiveCustomer>100);

        i++;
    }

    long e = sw.ElapsedMilliseconds; 

    return e;
}

The time cost is always less than 5

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
daniele3004
  • 13,072
  • 12
  • 67
  • 75
  • 2
    At no point do you actually execute the query. All you're measuring is whether it's faster to invoke a method once or to invoke it four times. I'm not surprised that once is faster. – David Sep 19 '14 at 13:38
  • IM kinda surprised. I thought LINQ would invoke itself 4 times quicker. stupid LINQ – DidIReallyWriteThat Sep 19 '14 at 13:41

1 Answers1

5

You are not even executing this LINQ query. You are just defining it.

If you'd for example use foreach, ToList() or Count() you'd get more meaningful results.

while (i < 20000)
{
    var result = list.Where(o => o.Id > 0
                            && o.Import > 0
                            && o.OrderConfirm==o.NumberConfirm
                            && o.IdActiveCustomer>100); // not executed
    int justToExecuteIt = result.Count();               // executed here
    i++;
}

There should not be a great difference between consecutive Wheres and consecutive &&.

I have asked a similar question recently: LINQ: differences between single Where with multiple conditions and consecutive Wheres with single condition

You might also find this question useful to understand the benefits of LINQ's deferred execution: What are the benefits of a Deferred Execution in LINQ?

Community
  • 1
  • 1
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • 1
    I am not sure, just asking. Does the difference between methods depend on whether the provider returns `IQueryable` or `IEnumerable`? In first case, multiple `Where` clauses would just modify the syntax tree, but in second, they would result in additional enumerations of the collection, which might be noticeable. Please correct me if I am wrong. UPD.:oh, I see that that Marc Gravell answered exactly that question on your link. – AndreySarafanov Sep 19 '14 at 13:43
  • 1
    @AndreySarafanov: even in LINQ-To-Objects multiple `Where` won't cause multiple enumerations due to the deferred exection. That's what my question linked above addressed. Btw, why the downvote? – Tim Schmelter Sep 19 '14 at 13:52
  • got it, thanks. The overhead caused by multiple enumerators and delegate invocations would be minor indeed. I am sorry for giving your answer a minus bacause of my wrong understanding of the subject. – AndreySarafanov Sep 19 '14 at 13:58
  • @AndreySarafanov: No problem! But you can undo up/downvotes if you want. Just click it again. – Tim Schmelter Sep 19 '14 at 14:01
  • The questions at the links really helped me, yes. And regarding the downvote, I tried to remove it, but unfortunately "You last voted on this answer 15 mins ago. Your vote is now locked in unless this answer is edited." – AndreySarafanov Sep 19 '14 at 14:03
  • I just cited the message this site showed me when I tried to change my vote. And now that you updated your answer, I am finally able to do it. [More](http://meta.stackexchange.com/questions/94567/you-last-voted-on-this-answer-6-mins-ago-locked-too-short) about it. – AndreySarafanov Sep 19 '14 at 14:10
  • 1
    +1, nice explanation Tim, still not sure about the downvote. – Habib Sep 19 '14 at 14:45