KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I was looking at someone sample code for async and noticed a few issues with the way it was implemented. Whilst looking at the code I wondered if it would be more efficient to loop through a list using as parallel, rather than just looping through the list normally. As far as I can tell there is very little difference in performance, both use up every processor, and both talk around the same amount of time to completed. This is the first way of doing it var tasks= Client.GetClients().Select(async p => await p.Initialize()); And this is the second var tasks = Client.GetClients().AsParallel().Select(async p => await p.Initialize()); Am I correct in assuming there is no difference between the two? The full program can be found below using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication2 { class Program { static void Main(string[] args) { RunCode1(); Console.WriteLine("Here"); Console.ReadLine(); RunCode2(); Console.WriteLine("Here"); Console.ReadLine(); } private async static void RunCode1() { Stopwatch myStopWatch = new Stopwatch(); myStopWatch.Start(); var tasks= Client.GetClients().Select(async p => await p.Initialize()); Task.WaitAll(tasks.ToArray()); Console.WriteLine("Time ellapsed(ms): " + myStopWatch.ElapsedMilliseconds); myStopWatch.Stop(); } private async static void RunCode2() { Stopwatch myStopWatch = new Stopwatch(); myStopWatch.Start(); var tasks = Client.GetClients().AsParallel().Select(async p => await p.Initialize()); Task.WaitAll(tasks.ToArray());
Tags (comma-separated)
Save Edits
Cancel