Search This Blog

Thursday, September 13, 2012

C# Parallel Convert a List<objects> to String with Custom Formatting

 

I wrote some code a while ago which did some processing, creating a list output, which I then had to turn into a String to TCP send it to a remote client.

It worked but took a while...

I was wondering why.... turns out the processing is quick as in parallel.. but the bottle neck was in the sending part...

Step 1... add a method to zip up the string and send that = slight performance increase as now sending a lot less data

Step 2... parallelise the list to string part...

It was like this.. and with about 40,000 items (egogridviewresult objects) in the list.. it'd take 20 or so seconds

for each (egogridviewresults v in r)
{
res += v.tarpt.X.ToString("0.00")+","+v.tarpt.Y.ToString("0.00")+","+v.cellx + "," + v.celly + "," + v.objid + "," + v.distance + "#";
}
res = Zip(res);
sendToClient(res);

- - - -  

I've now changed to this and it does the same job in about 2 seconds!

var results = new ConcurrentQueue<string>();

Parallel.ForEach(r, v =>
{
String r1 = v.tarpt.X.ToString("0.00") + "," + v.tarpt.Y.ToString("0.00") + "," + v.cellx + "," + v.celly + "," + v.objid + "," + v.distance.ToString("0.00");
results.Enqueue(r1);
});

res = String.Join("#", results);
res = Zip(res);
sendToClient(res);

__________________

Things learnt...

1) C# .NET 4 has a cool function String.Join which can concatenate a list into a string with a defined delimiter of your choice! 

2) Parallel.ForEach is just awesome!