Has Task Creation performance improved significantly in .NET 4.5?
-
Wednesday, March 28, 2012 2:38 PM
Hi There,
I would like to know what I will gain by using .NET 4.5 for scenarios that I use TPL especially when I create new tasks.
Bryan
- Edited by Bryan Robinsons Wednesday, March 28, 2012 4:28 PM
- Moved by Reed Copsey, JrMVP, Moderator Wednesday, March 28, 2012 4:37 PM Moved to TPL Forum (From:.NET Base Class Library)
All Replies
-
Wednesday, March 28, 2012 3:14 PM
TPL allows your program use System resources effectively. If you have set of operations that can be performed parallely, then TPL automatically schedule those operations in a multiprocessor system. See that you could do the same thing without TPL but you would require to write large code. So, by using TPL your code not only becomes small, but also becomes efficient.
http://blogs.msdn.com/b/pfxteam/archive/2009/10/06/9903475.aspx
http://msdn.microsoft.com/en-us/library/dd537609(v=vs.110).aspx
Please mark this post as answer if it solved your problem. Happy Programming!
-
Wednesday, March 28, 2012 3:43 PM
Hi Bryan,
TPL is designed to make it much easier to write managed code that can automatically use multiple processors. Using the library, you can conveniently express potential parallelism in existing sequential code, where the exposed parallel tasks will be run concurrently on all available processors. Usually this results in significant speedups.
Yo can find more information here:
http://msdn.microsoft.com/en-us/magazine/cc163340.aspx
Regards, http://shwetamannjain.blogspot.com
- Proposed As Answer by Shweta Jain Wednesday, March 28, 2012 3:43 PM
- Unproposed As Answer by Reed Copsey, JrMVP, Moderator Wednesday, March 28, 2012 4:37 PM
-
Wednesday, March 28, 2012 4:16 PMModerator
Hi There,
I would like to know what I will gain by using .NET 4.5 for scenarios that I use TPL especially when I create new tasks.
Geof
The TPL in general has had many improvements in .NET 4.5. I recommend reading the TPL Performance Improvements article referenced here: http://blogs.msdn.com/b/pfxteam/archive/2011/11/10/10235962.aspx
It goes into detail about many of the improvements that come with 4.5, especially in regards to performance of the TPL.
Reed Copsey, Jr. - http://reedcopsey.com
If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".- Proposed As Answer by Mansoor Omrani Wednesday, March 28, 2012 8:15 PM
- Marked As Answer by Stephen Toub - MSFTMicrosoft Employee, Owner Thursday, March 29, 2012 5:32 AM
-
Wednesday, March 28, 2012 4:20 PM
Hi Sweta and Adavesh,
Thanks for your answers. My question is not about "what is TPL?", my question is:
Has Task Creation performance improved significantly in .NET 4.5 comparing to the older .NET framework versions such .NET 4.0 ?
Regards,
Bryan
-
Wednesday, March 28, 2012 4:27 PM
Bryan,
You can write the following code and measure the performance by yourself:
publicstaticTuple<long,long> CreateTasks(intntasks){Task[] tasks =newTask[ntasks];Stopwatch sw =newStopwatch();Action action = () => { };longstartBytes = GC.GetTotalMemory(true);sw.Start();for(inti = 0; i < ntasks; i++) tasks[i] =newTask(action);sw.Stop();longendBytes = GC.GetTotalMemory(true);GC.KeepAlive(tasks);returnTuple.Create(sw.ElapsedMilliseconds,endBytes-startBytes);}You will 10~20 percent performance increase in average on different machines.
Regards,
Amir Ahani (MCSD.NET)
- Edited by Amir AhaniMVP Wednesday, March 28, 2012 4:29 PM
- Marked As Answer by Bryan Robinsons Wednesday, March 28, 2012 6:25 PM
-
Wednesday, March 28, 2012 6:25 PM
Thanks Amir and Reed.
I wrote the following code and I run it for both .NET 4.0 and .NET 4.5 on my machine, but surprisingly I receive the following results:
Test 1 -
In .NET 4.0, it takes 3351 Milli seconds, and in .NET 4.5, it takes 3570 Milli seconds
Test 2-
In .NET 4.0, it takes 3421 Milli seconds, and in .NET 4.5, it takes 3551 Milli seconds
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
CreateTasks(10000000);
}
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);
Console.WriteLine(" {0} .... {1} ", sw.ElapsedMilliseconds, endBytes - startBytes);
return Tuple.Create(sw.ElapsedMilliseconds, endBytes - startBytes);
}
}Please let me know what I am missing.
Thanks,
Bryan
-
Wednesday, March 28, 2012 6:42 PMModerator
Thanks Amir and Reed.
I wrote the following code and I run it for both .NET 4.0 and .NET 4.5 on my machine, but surprisingly I receive the following results:
Test 1 -
In .NET 4.0, it takes 3351 Milli seconds, and in .NET 4.5, it takes 3570 Milli seconds
Test 2-
In .NET 4.0, it takes 3421 Milli seconds, and in .NET 4.5, it takes 3551 Milli seconds
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
CreateTasks(10000000);
}
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);
Console.WriteLine(" {0} .... {1} ", sw.ElapsedMilliseconds, endBytes - startBytes);
return Tuple.Create(sw.ElapsedMilliseconds, endBytes - startBytes);
}
}Please let me know what I am missing.
Thanks,
Bryan
This isn't that good of a test, as you make a lot of Task instances, but don't actually start or use them....
A pure construction test like this is going to be fairly useless for real-world performance. (Also, I suspect you weren't running outside of VS - those numbers sound like the test host was attached...)
Reed Copsey, Jr. - http://reedcopsey.com
If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

