Tutorial :Variable scope and performance



Question:

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??


Solution:1

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.


Solution:2

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.


Solution:3

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.


Solution:4

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.


Solution:5

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.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »