Improving .NET #performance with Parallel.For
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! 🙂