tags:

views:

127

answers:

7

Below is the code i am using

private void TestFunction()
{
  foreach (MySampleClass c in dictSampleClass)
  {
    String sText = c.VAR1 + c.VAR2 + c.VAR3
    PerformSomeTask(sText,c.VAR4);
  }
}

My friend has suggesed to change to (to improve performance. dictSampleClass is a dictionary. It has 10K objects)

private void TestFunction()
{
  String sText="";
  foreach (MySampleClass c in dictSampleClass)
  {
    sText = c.VAR1 + c.VAR2 + c.VAR3
    PerformSomeTask(sText,c.VAR4);
  }
}

My Question is, "Does above change improve performance? if yes, how?"

WOW thats more than expected response. Most guys said "C# compiler would take care of that". So what about c compiler??

+2  A: 

Run the two functions through reflector and have a look at the generated assembly. It might give some insights but at best, the performance improval would be minimal.

cristobalito
A: 

No, it does not. Things like these are handled by the C# compiler which is very inteligent nad you basicaly do not need to care about these details.

Aways use code that promotes readability.

You can check this by disassembling the program.

A: 

I think its not. Same code written again above

Serkan Hekimoglu
A: 

The only thing you're saving here is generating a reference variable in stack but I'm pretty sure the compiler would have taken it out anyway.

Hasan Khan
+6  A: 

Hi,

The compiler has intelligence to move variable declarations into/out of loops where required. In your example however, you are using a string, which is immutable. By declaring it outside I believe you are trying to "create once, use many", however strings are created each time they are modified so you can't achieve that.

Not to sound harse, but this is a premature optimisation, and likely a failing one - due to string immutability.

If the collection is large, go down the route of appending many strings into a StringBuilder - separated by a delimiter. Then split the string on this delimiter and iterate the array to add them, instead of concatenating them and adding them in one loop.

StringBuilder sb = new StringBuilder();

foreach (MySampleClass c in dictSampleClass)
{
    sb.Append(c.VAR1);
    sb.Append(c.VAR2);
    sb.Append(c.VAR3);
    sb.Append("|");
}

string[] results = sb.ToString().Split('|');

for (int i = 0; i < dictSampleClass.Count; i++)
{
    string s = results[i];
    MySampleClass c = dictSampleClass[i];
    PerformSomeTask(s,c.VAR4); 
}

I infer no benefits to using this code - most likely doesn't even work!

UPDATE: in light of the fast performance of string creation from multiple strings, if PerformSomeTask is your bottleneck, try to break the iteration of the strings into multiple threads - it won't improve the efficiency of the code but you'll be able to utilise multiple cores.

Adam
I doubt it'll improve performance.
Manjoor
Ditto now realising that initialising a string from multiple strings is very quick.
Adam
A: 

Ii will not improve performance. But on another note: Assumed improvements are error prone. You have to measure you application and only optimise the slow part if needed. I don't believe that loop is your appliacations's bottleneck.

Code Clown
Not safe to assume that either, 10k items with string concatenations in a tight loop - could be memory hungry.
Adam
@Adam: It's not really a string concatenation in a loop, its a string initialisation from multiple sources. Its not the same as `String str = c.VAR1; str += c.VAR2; str += c.VAR3` which would cause performance problems.
ck
@ck true, I've never considered the performance differences of that.
Adam
+1  A: 

I'd go for this instead:

private void TestFunction() 
{ 
  foreach (MySampleClass c in dictSampleClass) 
  { 
    PerformSomeTask(c.VAR1 + c.VAR2 + c.VAR3, c.VAR4); 
  } 
} 

There's still probably no real performance benefit, but it removes creating a variable that you don't really need.

ck
Hmmmm.. My objective was only to know the diffrence not code optimization.
Manjoor