Il ne semble pas faire de squat pour le programme de test suivant. Est-ce parce que je teste avec une petite liste?Comment AsParallel fonctionne-t-il exactement?
static void Main(string[] args)
{
List<int> list = 0.UpTo(4);
Test(list.AsParallel());
Test(list);
}
private static void Test(IEnumerable<int> input)
{
var timer = new Stopwatch();
timer.Start();
var size = input.Count();
if (input.Where(IsOdd).Count() != size/2)
throw new Exception("Failed to count the odds");
timer.Stop();
Console.WriteLine("Tested " + size + " numbers in " + timer.Elapsed.TotalSeconds + " seconds");
}
private static bool IsOdd(int n)
{
Thread.Sleep(1000);
return n%2 == 1;
}
Les deux versions prennent 4 secondes à s'exécuter.
Comment avez-vous obtenu Upto, une méthode d'extension? – thewpfguy