Tim Barcz

Sponsors

The Lounge

Wicked Cool Jobs

Groups and Affiliations

Syndication

News

Task Parallel Library: Real-World Results

I’m working on a project that requires chewing through a lot of data. While looking for ways to make the code run faster – I hate waiting – I decided to throw the new Task Parallel Library at the problem to see what sort of improvements I could gain.  Below is the concept and the results I saw (remember: your mileage may vary).

The Code

Here is the gist of the original code:

var lines = File.ReadAllLines(path).ToList();
lines.ForEach(x => parsedResults.Add(lineParser.ParseLine(x)));

Quite simple for sure, but to run through 10,000 lines this was taking about 15,000 Milliseconds, or 15 seconds.  To me that felt slow, especially when you start talking about millions of rows (A million rows at 1.5 ms would still take just under 17 minutes).

Task Parallel Library Version (TPL)

There’s a very simple .ForEach method that’s quite handy and simple to use, however I have local variables and a master collection I need to combine into for later processing and therefore need to use a version of ForEach that supports thread-local variables.  Here is that code:

Parallel.ForEach(lines,
                                                       () => new List<LineResult>(),
                                                       (current, loop, threadLocalList) =>
                                                       {
                                                           threadLocalList.Add(lineParser.ParseLine(current));
                                                           return threadLocalList;
                                                       },
                                                       parsedResults.AddRange
                );

 

Results

Using the 10,000 line test version and taking an average of several runs here are the results:

Before: 15,267

After: 7,546

Speed improvement: 100%

Conclusion

Overall this isn’t a very exciting example, however it does show the potential impacts that the Task Parallel Library (TPL) can have on your runtime performance.  I was pleased with the library in that I had a very simple cast (looping) and there was very little to do. I simply added the “using” directive and that’s about it, no configuration or dependencies.  If you’ve played with the Task Parallel Library what real-world uses have you found?  What benefits have you seen from the code?


Posted 11-19-2010 12:47 AM by Tim Barcz
Filed under: ,

[Advertisement]

Comments

Alex wrote re: Task Parallel Library: Real-World Results
on 11-19-2010 7:11 AM

Nice!

Just a quick pointer: The speed improvement is 100%, not 50% (i.e., it previously averaged 0.67 lines per ms, it now averages 1.33 lines per ms, an improvement of 100%).

The time taken is 50%.

Sorry! :)

Tim Barcz wrote re: Task Parallel Library: Real-World Results
on 11-19-2010 8:06 AM

Alex,

Good catch. Fixed.

Thanks.

Harry M wrote re: Task Parallel Library: Real-World Results
on 11-19-2010 11:21 AM

You might get better results with File.ReadLines which is lazy evaluated and doesn't have to load the whole file into an array before processing it.

Not sure how that will roll with the Parallel library though

curlyfro wrote re: Task Parallel Library: Real-World Results
on 11-22-2010 4:59 PM

i'm assuming your machine is a dual-core.  this scales if you have multiple cores, right?

Add a Comment

(required)  
(optional)
(required)  
Remember Me?

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco <-- NEW Friend!
NServiceBus <-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)