0

Which one of this syntax have better performance and speed in searching between data?

First alternative:

this.Message = pageContentsli
    .Where(m => m.PName == "Message")
    .First()
    .ContentValue;

Second alternative:

  foreach (PageContentsModel pc in pageContentsli)
  {
     if (pc.PName == "Message"){
        this.Message = pc.ContentValue;
        break;
      }
  }
jgauffin
  • 99,844
  • 45
  • 235
  • 372
motevalizadeh
  • 5,244
  • 14
  • 61
  • 108
  • 1
    Do you have actual performance issues? – Bart Friederichs Jul 02 '13 at 10:31
  • 6
    From http://ericlippert.com/2012/12/17/performance-rant/ _If you have two horses and you want to know which of the two is the faster then_ **race your horses.** – Soner Gönül Jul 02 '13 at 10:32
  • i want to know which one have higher speed in searching data – motevalizadeh Jul 02 '13 at 10:32
  • Do you have a stopwatch? – David Heffernan Jul 02 '13 at 10:32
  • 4
    They both continue unnecessarily after finding the first match. – Matthew Watson Jul 02 '13 at 10:32
  • @DavidHeffernan everyone has a Stopwatch... – Vogel612 Jul 02 '13 at 10:33
  • This question is confusing. If you're comparing the two code blocks, one of them isn't valid syntax. If you're comparing the two statements in the second code block, they don't do the same thing, so it's not valid to compare them by performance. – BoltClock Jul 02 '13 at 10:33
  • 1
    The foreach will be _a lot_ slower when you're accessing a datasource through Entity Framework, for example, as it will walk over all items. Linq to Entities will write a nice WHERE query. The answer in cases like this always is: it depends, benchmark it yourself. - @Vogel612 source please? See [Is it a bad practice to use break in a for loop?](http://stackoverflow.com/questions/3922599/is-it-a-bad-practice-to-use-break-in-a-for-loop) for example. – CodeCaster Jul 02 '13 at 10:42
  • 1
    @CodeCaster was speaking in the more complicated direction. even though you are correct, that break in easy loops is quite acceptable and easier to understand, i would say it is quite easy to mistake "acceptable" as "good" when not confronted with the problems of the usage in complicated (nested) loops. thus i prefer calling it "bad practice" or "last resort", for the sake of code-readability in the OP's own favor... but now my graceperiod for editing ran out and i can't fix that.... – Vogel612 Jul 02 '13 at 10:47

2 Answers2

2

Before continuing with the comparison, you should move the condition inside First like this:

this.Message = pageContentsli.First(m => m.PName == "Message").ContentValue;

As far as the performance goes, you should see no difference. On readability, however, the modified LINQ version wins hands down.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

First of all let me explain what both syntax actually does.

In the first syntax (where), it process the element one by one and if it catches m.PName="Message", it returns the requested value to this.Message and then the processing stops.

In the second syntax (foreach), it process the element one by one and when it catches m.PName="Message", it assigns the values to this.Message and again it starts processing for the rest of the element.

So eventhough the value is found, the second syntax doesn't stop and so it takes more time to complete than the first syntax.

Shafiq Abbas
  • 484
  • 1
  • 5
  • 18