.NET Zone is brought to you in partnership with:

Amir Ahani (Microsoft Most Valuable Professional) is expert in Microsoft .NET technologies such as C# and he spends most of his time teaching and consulting internationally. He has been teaching advanced Microsoft courses at British Columbia Institute of Technology (BCIT) since 2003. In addition, he is a member of and speaker at the following communities: DevTeach, .netBC, Vancouver Technology Festival, ASQ, APICS, PMI, NPA, and ITAC. Amir is a DZone MVB and is not an employee of DZone and has posted 12 posts at DZone. You can read more from them at their website. View Full User Profile

Task Creation Performance in .NET 4.5

06.03.2012
| 1636 views |
  • submit to reddit

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.

Published at DZone with permission of Amir Ahani, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)