Over a million developers have joined DZone.

Task Creation Performance in .NET 4.5

DZone's Guide to

Task Creation Performance in .NET 4.5

· ·
Free Resource

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);
    for (int i = 0; i < ntasks; i++) tasks[i] = new Task(action);
    long endBytes = GC.GetTotalMemory(true);
    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

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}