I have a for loop that creates 4 tasks and each task prints its loop index - simple program to test performance
I have an outside loop that run the above loop 1000 times (iterations) I wanted to check the performance of TASKs and Threads
(1) Test 1:I thought this would create TASKs (not threads) only but I found it uses TPL
tasks[i] = Task.Factory.StartNew(() => Console.WriteLine(tmp));
(2) I rewrote with the TaskCreationOptions.LongRunning as follows
tasks[i] = Task.Factory.StartNew(() => Console.WriteLine(tmp), TaskCreationOptions.LongRunning);
(3) Then I tried to test THREADs not task using the same code as above but now using "new Thread" instead of factory
for (int i = 0; i < 4; i++)
{
var tmp = i;
tasks[i] = new Thread(new ThreadStart(() => Console.WriteLine(tmp)));
tasks[i].Start();
tasks[i].Join();
}
The timing results showed the best performance is (2), then (3), then (1)
Please can one explain the reasonsof the performance results, and explain which one of the above truly just a task (an O.S. Process) and which are using threads?
I tried to use the profiler, but only have access to Visual Studio 2010 Professional and it appears the profiler comes only with the permium or ultimate version.
Tasks andProcesses are completely different things. And if you want to run some code, you have to use some thread. - svick