Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Benchmarking Task Creation Performance in .NET 4.5 vs .NET 4.0

DZone's Guide to

Benchmarking Task Creation Performance in .NET 4.5 vs .NET 4.0

·
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);
    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.

Topics:

Published at DZone with permission of Amir Ahani, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

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

{{ parent.tldr }}

{{ parent.urlSource.name }}