Here i attached my example that i used for performance test. Why there is so much diffrence between all this ? (This is sample console application)
class Program
{
internal class ThreadObj
{
public ManualResetEvent signalComplete { get; set; }
public int TaskItem { get; set; }
}
static void ThreadWork(object o)
{
ThreadObj obj = (ThreadObj)o;
System.Threading.Thread.Sleep(5000);
obj.signalComplete.Set();
}
static void Main(string[] args)
{
// Using new .net 4.0 Task
Stopwatch watch = new Stopwatch();
watch.Start();
System.Collections.Concurrent.ConcurrentBag<Task> tasks = new System.Collections.Concurrent.ConcurrentBag<Task>();
Parallel.For(0, 60, i =>
{
Task t = Task.Factory.StartNew(() =>
{
System.Threading.Thread.Sleep(5000);
}, TaskCreationOptions.PreferFairness);
tasks.Add(t);
});
Console.WriteLine("Waiting for task to finish");
Task.WaitAll(tasks.ToArray());
watch.Stop();
Console.WriteLine("Complete(Tasks) : Time " + watch.ElapsedMilliseconds.ToString());
// Using Thread
watch.Reset();
watch.Start();
System.Collections.Concurrent.ConcurrentBag<ManualResetEvent> tasksThreads = new System.Collections.Concurrent.ConcurrentBag<ManualResetEvent>();
Parallel.For(0, 60, i =>
{
ManualResetEvent signal = new ManualResetEvent(false);
tasksThreads.Add(signal);
Thread t = new Thread(new ParameterizedThreadStart(ThreadWork));
t.Start(new ThreadObj() { signalComplete = signal, TaskItem = i });
});
Console.WriteLine("Waiting for task to finish");
WaitHandle.WaitAll(tasksThreads.ToArray());
watch.Stop();
Console.WriteLine("Complete(Threads) : Time " + watch.ElapsedMilliseconds.ToString());
// Using ThreadPool
watch.Reset();
watch.Start();
System.Collections.Concurrent.ConcurrentBag<ManualResetEvent> tasksThreadPools = new System.Collections.Concurrent.ConcurrentBag<ManualResetEvent>();
Parallel.For(0, 60, i =>
{
ManualResetEvent signal = new ManualResetEvent(false);
tasksThreadPools.Add(signal);
ThreadObj obj = new ThreadObj() { signalComplete = signal, TaskItem = i };
ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadWork), obj);
});
Console.WriteLine("Waiting for task to finish");
WaitHandle.WaitAll(tasksThreadPools.ToArray());
watch.Stop();
Console.WriteLine("Complete(ThreadPool) : Time " + watch.ElapsedMilliseconds.ToString());
Console.ReadLine();
}
}
Please provide your suggetion on this.
Here is sample output that i got.
Waiting for task to finish
Complete(Tasks) : Time 28050
Waiting for task to finish
Complete(Threads) : Time 5435
Waiting for task to finish
Complete(ThreadPool) : Time 15032