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?

bookmarking submission wrote re: Task Parallel Library: Real-World Results
on 01-17-2013 7:41 PM

fi1woa Really informative blog article. Awesome.

discount generic cialis wrote re: Task Parallel Library: Real-World Results
on 01-27-2013 4:12 AM

Lrlc4G Say, you got a nice post. Fantastic.

weight loss pills wrote re: Task Parallel Library: Real-World Results
on 02-01-2013 5:26 PM

aIHBpp I value the blog.Really looking forward to read more. Really Cool.

buy viagra online wrote re: Task Parallel Library: Real-World Results
on 02-03-2013 6:01 PM

Inqwkz Very good article.Really thank you! Keep writing.

buy stendra generic wrote re: Task Parallel Library: Real-World Results
on 02-15-2013 6:11 PM

3TV01W This is one awesome article.Really looking forward to read more. Fantastic.

buy cialis discount wrote re: Task Parallel Library: Real-World Results
on 02-24-2013 2:48 PM

PKZ9jT Really enjoyed this blog article.Much thanks again. Awesome.

http://clomidnoprescription.beep.com/ wrote re: Task Parallel Library: Real-World Results
on 02-27-2013 11:22 PM

glt32j Really enjoyed this blog.Much thanks again. Fantastic.

bookmarks wrote re: Task Parallel Library: Real-World Results
on 03-13-2013 12:51 PM

d5Te7H Enjoyed every bit of your blog.Much thanks again. Want more.

bookmarks wrote re: Task Parallel Library: Real-World Results
on 03-14-2013 8:24 PM

fLgpSo Thanks-a-mundo for the blog post. Really Great.

Social bookmarks wrote re: Task Parallel Library: Real-World Results
on 03-23-2013 8:03 PM

tfP62O I really like and appreciate your blog.Thanks Again. Much obliged.

funny shirts wrote re: Task Parallel Library: Real-World Results
on 04-06-2013 4:08 PM

Very neat article post.Really thank you! Much obliged.

Social bookmarks wrote re: Task Parallel Library: Real-World Results
on 04-14-2013 6:47 AM

yJmLBI Im grateful for the article post.Thanks Again.

buy social bookmarks wrote re: Task Parallel Library: Real-World Results
on 04-19-2013 4:04 PM

P2zfAH I loved your blog.Really looking forward to read more. Really Cool.

buy social bookmarks wrote re: Task Parallel Library: Real-World Results
on 04-28-2013 5:22 AM

VtZaKd Im grateful for the article post.Really thank you! Fantastic.

slr lenses wrote re: Task Parallel Library: Real-World Results
on 05-14-2013 3:22 PM

iqWcOJ wow, awesome blog post. Want more.

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
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)