I have sample code to compare processing time for Parallel approach and Task approach. The goal of this experiment is understanding of how do they work.
So my questions are:
Does Task is just a wrap for ThreadPool.QueueUserWorkItem method?
public Task SomeLongOperation() { return Task.Delay(3000); } static void Main(string[] args) { Program p = new Program(); List<Task> tasks = new List<Task>(); tasks.Add(Task.Factory.StartNew(() => p.SomeLongOperation())); tasks.Add(Task.Factory.StartNew(() => p.SomeLongOperation())); var arr = tasks.ToArray(); Stopwatch sw = Stopwatch.StartNew(); Task.WaitAll(arr); Console.WriteLine("Task wait all results: " + sw.Elapsed); sw.Stop(); sw = Stopwatch.StartNew(); Parallel.Invoke(() => p.SomeLongOperation(), () => p.SomeLongOperation()); Console.WriteLine("Parallel invoke results: " + sw.Elapsed); sw.Stop(); Console.ReadKey(); }
Here are my processing results:
EDIT:
Changed code to look like this:
Program p = new Program(); Task[] tasks = new Task[2]; Stopwatch sw = Stopwatch.StartNew(); tasks[0] = Task.Factory.StartNew(() => p.SomeLongOperation()); tasks[1] = Task.Factory.StartNew(() => p.SomeLongOperation()); Task.WaitAll(tasks); Console.WriteLine("Task wait all results: " + sw.Elapsed); sw.Stop(); sw = Stopwatch.StartNew(); Parallel.Invoke(() => p.SomeLongOperation(), () => p.SomeLongOperation()); Console.WriteLine("Parallel invoke results: " + sw.Elapsed); sw.Stop();
My new results:
EDIT 2: When I replaced code with Parallel.Invoke to be first and Task.WaitAll to be second the situation has been changed cardinally. Now Parallel is slower. It makes me think of incorrectness of my estimates. I changed code to look like this:
Program p = new Program(); Task[] tasks = new Task[2]; Stopwatch sw = null; for (int i = 0; i < 10; i++) { sw = Stopwatch.StartNew(); Parallel.Invoke(() => p.SomeLongOperation(), () => p.SomeLongOperation()); string res = sw.Elapsed.ToString(); Console.WriteLine("Parallel invoke results: " + res); sw.Stop(); } for (int i = 0; i < 10; i++) { sw = Stopwatch.StartNew(); tasks[0] = Task.Factory.StartNew(() => p.SomeLongOperation()); tasks[1] = Task.Factory.StartNew(() => p.SomeLongOperation()); Task.WaitAll(tasks); string res2 = sw.Elapsed.ToString(); Console.WriteLine("Task wait all results: " + res2); sw.Stop(); }
And here are my new results:
Now I can suggest that this experiment is much more clear. The results are almost the same. Sometimes Parallel and sometimes Task is faster. Now my questions are:
1. Where should I use Task and where Parallel?
2. What benefits of using Task in comparison to Parallel?
3. Does Task is just a wrap for ThreadPool.QueueUserWorkItem method?
Any helpful info that can clarify those questions are welcome.
EDIT as of this article from MSDN:
Both Parallel and Task are wrappers for ThreadPool. Parallel invoke also awaits until all tasks will be finished.
Related to your questions:
Using Task, Parallel or ThreadPool depends on the granularity of control you need to have on the execution of your parallel tasks. I'm personally got used to Task.Factory.StartNew()
, but that's a personal opinion. The same relates to ThreadPool.QueueUserWorkItem()
Additional Information: The first call to Parallel.Invoke() and Task.Factory.StartNew() might be slower due to internal initialization.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With