The only way to know for sure is to test and this can change according to the data set and the algorithm to be run on each item.
First let’s note that the two algorithms shown are not equivalent. One takes one data unconditionally and the other takes it conditionally. When you buy things that do different tasks you can’t choose what you do faster, you have to choose what does what you want it to do in the situation.
So let’s compare the same things, between:
foreach(var i in lista) {
if (i.NmCampo == "MeuNome") {
var teste = i.NmCampo;
break;
}
}
var teste = lista.Select(campo => campo.NmCampo).FirstOrDefault(c => c == "MeuNome");
The first is clearly faster because it has less abstraction, less indirect, less real code to run.
The second form may be useful but it is more complicated internally. Whether it should be used as such is a matter of taste. Some people would only choose the first one if the performance really makes a difference, and others would only choose LINQ if it showed something more advantageous.
But the first is too big, has a more complicated logic. It may be that legibility is better to choose the second. But it’s still a matter of taste. I’ve already talked about the subject at another answer.
Another important point to note is that it is easy to forget that the break
must be there inside the if
to shut it down when you find what you want and behave exactly as in FirstOrDefault
and the execution of foreach
would be slower in most cases because it would have to scan the entire list and with the LINQ expression. But again, the algorithm would be different. It is important to note that not everyone understands all the behavior of the methods available for use with LINQ and all its implications. Hiding a logic isn’t always a good idea. It can make it difficult to understand who is using it if it does not know what is written there. Explicit codes can be better for those who do not know the existing abstractions.
I did a test to see the difference, there’s no point in talking about performance theories because they can prove false in practice or they can be partially true. At a minimum they do not provide enough information to make a conscious decision:
Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference. One of the results:
Resultado do for: MeuNome
17497
Resultado do foreach: MeuNome
16669
Resultado do LINQ: MeuNome
57009
Agora acha no primeiro
Resultado do for: MeuNome
12
Resultado do foreach: MeuNome
3
Resultado do LINQ: MeuNome
6599
We can see that the for
and the foreach
are almost equivalent and it is not possible to see expressive difference. And contrary to popular belief, the foreach
may be faster than the for
in some situations (in some tests I’ve run it was faster despite derisory difference). We also note that LINQ is much slower. It is not a trivial difference. If you want performance, if it is necessary, run away from LINQ.
When a LINQ expression will be used it must be compiled and this takes a lot of time, so to find the first element in the list is brutally (on average 1000 times) slower than making a loop with a filter inside. Of course having 7000 ticks is nothing but numerically speaking the difference is huge.
Of course this depends on the situation as I said before and needs to analyze whether it will make real difference.
Note that the variable
teste
has different values in each case. In the first, the variableteste
will take the valueNmCampo
in each iteration offoreach
. In the second caseteste
is with the value of the first line that returns results (for example, in 100 rows of results, the variable will only be with the value of the first line)– CesarMiguel
@Cesarean Michael, this happens due to Firstordefaul, but I can suppress if I need to get a collection. The example is hypothetical only to illustrate what has the best performance. Of course each situation should be studied carefully.
– pnet
right. I just found it odd two examples that did different things :P Which one has better performance? I’m not sure, but I think licks. But I have no grounds to prove it
– CesarMiguel
Take a look at this topic http://stackoverflow.com/questions/1044236/nested-foreach-vs-lambda-linq-query-performancelinq-to-objects I think this will be useful
– Randrade
@Cesarmiguel If I have to sweep everything
foreach
is always faster. Performance depends on the internal algorithm and dataset. If the LINQ expression had only theSelect
she would certainly be slower.– Maniero
By the Renilson link and now by the bigown, really the performance with Foreach in most cases is better, of course there may be situations where the use of LINQ would be convenient, but the foreach scan seems to perform better.
– pnet
@pnet put a real test to show the difference
– Maniero