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.

social bookmarking service wrote re: Task Parallel Library: Real-World Results
on 06-06-2013 4:06 AM

GUsZgg I really enjoy the post.Really thank you! Will read on...

cheap social bookmarks wrote re: Task Parallel Library: Real-World Results
on 06-19-2013 7:24 PM

z6E42I A big thank you for your blog post.Much thanks again.

social bookmarks wrote re: Task Parallel Library: Real-World Results
on 06-20-2013 11:58 AM

tT4EZO Hey, thanks for the article. Much obliged.

news and many more wrote re: Task Parallel Library: Real-World Results
on 07-04-2013 9:39 AM

CZHRgf Hey, thanks for the post. Cool.

cool news wrote re: Task Parallel Library: Real-World Results
on 07-08-2013 9:17 PM

4UULbe Thank you ever so for you blog post.Much thanks again. Much obliged.

super news wrote re: Task Parallel Library: Real-World Results
on 07-12-2013 1:43 AM

Z03Csw I really liked your blog article.Really thank you! Want more.

this site wrote re: Task Parallel Library: Real-World Results
on 07-25-2013 2:26 PM

Thanks for the article.Really looking forward to read more. Awesome.

news wrote re: Task Parallel Library: Real-World Results
on 07-26-2013 3:16 AM

o2jq0M I value the post.Really thank you!

buy social bookmarks wrote re: Task Parallel Library: Real-World Results
on 07-28-2013 6:31 PM

sTNto3 Looking forward to reading more. Great blog post.Really looking forward to read more. Much obliged.

amazing news wrote re: Task Parallel Library: Real-World Results
on 08-02-2013 11:14 PM

sAYip4 I am so grateful for your article post.Thanks Again. Really Cool.

best news of them all wrote re: Task Parallel Library: Real-World Results
on 08-04-2013 5:05 PM

fAigYI Thanks for the post.Really thank you! Really Great.

great link buildng wrote re: Task Parallel Library: Real-World Results
on 08-19-2013 8:27 AM

RlNWpm Very good blog post. Keep writing.

great link buildng wrote re: Task Parallel Library: Real-World Results
on 08-19-2013 8:23 PM

vWbx7s A big thank you for your blog article.Much thanks again. Will read on...

great link buildng wrote re: Task Parallel Library: Real-World Results
on 08-21-2013 2:04 PM

iwCvBi Im obliged for the article.Much thanks again. Much obliged.

great seo service wrote re: Task Parallel Library: Real-World Results
on 09-04-2013 2:40 AM

sFz24b Im grateful for the blog article.Really thank you! Want more.

link building wrote re: Task Parallel Library: Real-World Results
on 09-06-2013 12:42 AM

UV5If2 Thanks for sharing, this is a fantastic article post. Will read on...

online business wrote re: Task Parallel Library: Real-World Results
on 09-12-2013 8:34 PM

UQqEEI I cannot thank you enough for the article.Much thanks again. Will read on...

pro link building wrote re: Task Parallel Library: Real-World Results
on 09-24-2013 11:55 AM

HrcQgZ Major thankies for the blog article.Thanks Again. Really Cool.

link building team wrote re: Task Parallel Library: Real-World Results
on 10-01-2013 1:43 AM

5JcYoZ Thank you ever so for you blog. Really Cool.

best link build wrote re: Task Parallel Library: Real-World Results
on 10-16-2013 5:49 AM

syIpNg Awesome post.Really thank you! Much obliged.

link building wrote re: Task Parallel Library: Real-World Results
on 10-25-2013 11:00 PM

wKgcSw Very informative blog post.Really looking forward to read more. Will read on...

link building wrote re: Task Parallel Library: Real-World Results
on 10-31-2013 10:02 PM

PD8kVL Looking forward to reading more. Great article post.Really thank you! Really Cool.

awesome stuff here wrote re: Task Parallel Library: Real-World Results
on 11-17-2013 12:24 PM

csYWKR Awesome article.Really looking forward to read more. Cool.

awesome site wrote re: Task Parallel Library: Real-World Results
on 11-20-2013 7:09 AM

p8PYyq Really informative blog post.Thanks Again. Will read on...

seo service wrote re: Task Parallel Library: Real-World Results
on 12-15-2013 9:37 AM

kcs5sX A round of applause for your article post.Really thank you! Want more.

nice site here wrote re: Task Parallel Library: Real-World Results
on 01-18-2014 12:08 AM

I7smQT I value the article post.Really thank you! Awesome.

smashing site wrote re: Task Parallel Library: Real-World Results
on 03-12-2014 1:05 PM

sFPSxG wow, awesome article post.Really thank you!

best prices wrote re: Task Parallel Library: Real-World Results
on 03-22-2014 5:00 PM

FSIMgd A round of applause for your blog article.Much thanks again. Will read on...

check this out now wrote re: Task Parallel Library: Real-World Results
on 03-25-2014 4:50 PM

eM97Us Thanks-a-mundo for the blog.Really thank you! Keep writing.

seo for cheap wrote re: Task Parallel Library: Real-World Results
on 04-01-2014 9:21 AM

nFl1pJ I appreciate you sharing this post.Really looking forward to read more. Awesome.

check it out wrote re: Task Parallel Library: Real-World Results
on 04-01-2014 9:23 AM

DXTZWp Muchos Gracias for your article.Really looking forward to read more. Cool.

stunning service wrote re: Task Parallel Library: Real-World Results
on 04-04-2014 4:37 PM

RkBW84 Really enjoyed this blog post. Great.

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)