Dokonałem pewnych optymalizacji algorytmu, który znajdzie najmniejszą liczbę, która jest większa niż X, w danej tablicy, ale potem natknąłem się na dziwną różnicę. Na poniższym kodzie "ForeachUpper" kończy się 625ms, a "ForUpper" kończy się, jak sądzę, kilkoma godzinami (szalenie wolniej). Dlaczego tak?C# - For vs Foreach - Ogromna różnica wydajności
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);
}
Dzięki
wierzę, że jest to możliwe powielać Kliknij tutaj, aby zobaczyć [Duplikat] [1] [1]: http://stackoverflow.com/questions/44220/difference-between- Foreach-and-for-Loops-over-an -ienumerable-in-c-sharp – SpaceApple
BTW, użyj klasy 'Stopwatch'. – SLaks
dlaczego "przełamujesz" z "jeśli"? – Ofiris