我對一個算法進行了一些優化,該算法在給定的數組中找到了大於X的最小數字,但後來我偶然發現了一個奇怪的差異。在下面的代碼中,「ForeachUpper」在625ms結束,「ForUpper」結束,我相信,幾個小時(瘋狂地慢)。爲什麼這樣?C# - 對於VS Foreach - 巨大的性能差異
class Teste
{
public double Valor { get; set; }
public Teste(double d)
{
Valor = d;
}
public override string ToString()
{
return "Teste: " + Valor;
}
}
private static IEnumerable<Teste> GetTeste(double total)
{
for (int i = 1; i <= total; i++)
{
yield return new Teste(i);
}
}
static void Main(string[] args)
{
int total = 1000 * 1000*30 ;
double test = total/2+.7;
var ieTeste = GetTeste(total).ToList();
Console.WriteLine("------------");
ForeachUpper(ieTeste.Select(d=>d.Valor), test);
Console.WriteLine("------------");
ForUpper(ieTeste.Select(d => d.Valor), test);
Console.Read();
}
private static void ForUpper(IEnumerable<double> bigList, double find)
{
var start1 = DateTime.Now;
double uppper = 0;
for (int i = 0; i < bigList.Count(); i++)
{
var toMatch = bigList.ElementAt(i);
if (toMatch >= find)
{
uppper = toMatch;
break;
}
}
var end1 = (DateTime.Now - start1).TotalMilliseconds;
Console.WriteLine(end1 + " = " + uppper);
}
private static void ForeachUpper(IEnumerable<double> bigList, double find)
{
var start1 = DateTime.Now;
double upper = 0;
foreach (var toMatch in bigList)
{
if (toMatch >= find)
{
upper = toMatch;
break;
}
}
var end1 = (DateTime.Now - start1).TotalMilliseconds;
Console.WriteLine(end1 + " = " + upper);
}
感謝
我相信這是一個可以在這裏重複 點擊查看[複製] [1] [1]:http://stackoverflow.com/questions/44220/difference-between- foreach-for-loops-over-an-enumerable-class-in-c-sharp – SpaceApple 2013-03-04 15:27:13
順便說一句,使用'Stopwatch'類。 – SLaks 2013-03-04 15:27:45
你爲什麼從'if'中斷? – Ofiris 2013-03-04 15:28:18