Task Creation Performance in .NET 4.5
In this post, I will compare the Task creation performance in .NET 4 and .NET 4.5.
I will measure both time and memory consumption associated with Task creation:
public static Tuple<long, long> CreateTasks(int ntasks) { Task[] tasks = new Task[ntasks]; Stopwatch sw = new Stopwatch(); Action action = () => { }; long startBytes = GC.GetTotalMemory(true); sw.Start(); for (int i = 0; i < ntasks; i++) tasks[i] = new Task(action); sw.Stop(); long endBytes = GC.GetTotalMemory(true); GC.KeepAlive(tasks); return Tuple.Create(sw.ElapsedMilliseconds,endBytes-startBytes); }
The results on my test machine are as follows:
The benchmark results do indeed show the smaller footprint of a Task in .NET 4.5, in addition to the decreased amount of time that it takes to create Tasks.