达网络 - 如果是名单< T> .ForEach者优先通过一个标准的foreach循环? [英] .Net - When is List<T>.ForEach prefered over a standard foreach loop?
问题描述
的泛型列表类有一个 .ForEach(动作< T>动作)
方法。现在,我已经做了一些的简单的他们俩是如何执行的时序,它似乎是通用的ForEach是较差的表演。该(摘录编译器友好的)代码如下 -
The generic list class has a .ForEach(Action<T> action)
method. Now i've done some simple timings of how they both perform and it seems that the generic ForEach is the poorer performer. The (Snippet Compiler Friendly) code is below -
public static class timer{
public static long foreachloop = 0;
public static long Gforeachloop = 0;}
public class something{
public List<string> myStrings = new List<string>();
public something()
{
for(int i = 1; i<=5000000;i++)
{
myStrings.Add(i.ToString());
}
}}
public class cls1{
private static List<string> Strings = new List<string>();
private static List<string> OtherStrings = new List<string>();
public static void RunSnippet()
{
something s = new something();
Stopwatch watch = new Stopwatch();
watch.Start();
foreach(string x in s.myStrings)
{
Strings.Add(x);
}
watch.Stop();
timer.foreachloop = watch.ElapsedMilliseconds;
watch.Reset();
watch.Start();
s.myStrings.ForEach(delegate(string n){OtherStrings.Add(n);});
s.myStrings.Clear();
watch.Stop();
timer.Gforeachloop = watch.ElapsedMilliseconds;
WL("FOREACH-"+timer.foreachloop + ",Count = " + Strings.Count);
WL("GFOREACH-"+timer.Gforeachloop + ",Count = " + OtherStrings.Count);
}
#region Helper methods
public static void Main()
{
try
{
RunSnippet();
}
catch (Exception e)
{
string error = string.Format("---\nThe following error occurred while executing the snippet:\n{0}\n---", e.ToString());
Console.WriteLine(error);
}
finally
{
Console.Write("Press any key to continue...");
Console.ReadKey();
}
}
private static void WL(object text, params object[] args)
{
Console.WriteLine(text.ToString(), args);
}
private static void RL()
{
Console.ReadLine();
}
private static void Break()
{
System.Diagnostics.Debugger.Break();
}
#endregion
}
FOREACH出来在177ms和GFOREACH在707ms。
FOREACH comes out at 177ms and GFOREACH at 707ms.
现在我猜有一个很好的理由使用它,但我只是想不出之一。很明显的表现是不是这个原因所以,问题是什么时候会是最好的选择?
Now I'm guessing there's a good reason for using it but i just can't think of one. Clearly performance isn't the reason so the question is when would it be the best option?
感谢。
推荐答案
这从埃里克利珀博客文章介绍了相关背景:
This blog post from Eric Lippert gives the background:
的 http://blogs.msdn.com/ericlippert/archive/2009/05/18/foreach-vs -foreach.aspx
他在谈论一个扩展方法的共同建议,做同样的事情的IEnumerable< T>
,但哲学异议适用于列表< T> .ForEach
以及
He's talking about the common suggestion of an extension method to do the same thing for IEnumerable<T>
, but the philosophical objection applies to List<T>.ForEach
as well.
这表明,也许这方法是从来没有这样一个好主意,虽然看起来酷。它更清晰只使用的foreach
。
This suggests that maybe that method was never such a good idea, although it looks "cool". It's clearer to just use foreach
.
我认为,这样的方法可以被认为是的一个的经典修复闭合过循环变量错误。
I've suggested that such methods can be thought of as a fix for the classic closure-over-loop-variable bug.
但在实践中我刚刚拿到了地察觉这种虫子更好。
But in practice I've just got better at spotting such bugs.
这篇关于达网络 - 如果是名单< T> .ForEach者优先通过一个标准的foreach循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!