Home > Uncategorized > Improving .NET #performance with Parallel.For

Improving .NET #performance with Parallel.For

tuning-performance

If you ever have a bit of code that does this;

var strSoughtUrl = “”
for each url in urls
var blnFoundIt = MakeWebRequestAndFindSomething(url);
if (blnFoundIt)
{
strSoughtUrl = url
}
next

That is to say, loops through a list of Urls, makes a web request to each one, and then breaks when it finds whatever you were looking for –

Then you can greatly improve the speed of the code using Parellel.for to something like this;

var resultCollection = new ConcurrentBag<string>();
Parallel.For(0, urls.Count, (i,loopstate) =>
{
var url = urls[(int)i];
var blnFoundIt = MakeWebRequestAndFindSomething(url);
if (blnFoundIt)
{
resultCollection.Add(url);
loopstate.Stop();
}
});
var strSoughtUrl = resultCollection.ToArray().First()

I have seen this code speeding up from 22 seconds to 5 seconds when searching through about 100 urls, since most of the time spent requesting a url is waiting for another machine to send all it’s data.

This is yet another performance increase for the Car Registration API service! 🙂

Advertisements
Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: