3

Is there any noticeable benefit regarding performance on using a constant over a string. Suppose the following case

public void MethodUsingConstant()
{
  const string searchedText = "foo";

  //some operations using searchedText string
}

public void MethodNoUsingConstant()
{
  string searchedText = "foo";

  //some operations using searchedText string
}

There is also the option to take this to a class-struct constant field.

Or, should I avoid to overthink too much these micro optimizations?

monsieurgutix
  • 137
  • 1
  • 7
  • 3
    Optimize only when you have a demonstrable, measurable performance problem. Even then, find the performance bottlenecks and only optimize them. – Kevin Sep 11 '17 at 20:22
  • This would be a very extreme "optimization" if any - both strings would be interned and you would be getting the same reference at runtime anyway. If you compile both your examples with optimizations and look at the IL, they are the same. – Scott Sep 11 '17 at 20:28
  • 3
    There is a difference in regards program correctness. By declaring as a constant, your intention is clear: this is a static value, not a variable that could be set to a different value halfway down the code. – hatchet - done with SOverflow Sep 11 '17 at 20:31
  • Side note: `const string` doesn't mean the string is immutable, it means the variable is immutable. It's still possible to change the value of the string in a `const` variable at runtime. hatchet is right - it ultimately comes down to correctness. – Scott Sep 11 '17 at 20:43
  • 1
    @Scott: _"It's still possible to change the value of the string"_ -- not within the constraints of standard, safe C# code. The `string` type is immutable. To change the value of an instance of `string` would require intentionally bypassing language semantics with reflection or other nefarious means. – Peter Duniho Sep 11 '17 at 20:46
  • Possible duplicate of [Advantages of using const instead of variables inside methods](https://stackoverflow.com/questions/5833912/advantages-of-using-const-instead-of-variables-inside-methods) – hatchet - done with SOverflow Sep 11 '17 at 21:11
  • 1
    There is a single optimization that is never premature: Optimizing for readability. If a variable is intended to be immutable, convey that intent by making it `const`. @hatchet: The program is correct either way. There is no difference with respect to program correctness at all. – IInspectable Sep 11 '17 at 21:25
  • @IInspectable - I was thinking of future correctness. If intention is clear, it reduces the chances of a second programmer coming in and introducing an error. – hatchet - done with SOverflow Sep 11 '17 at 21:55
  • @hatchet: A future update to the code could well assign a new string instance to the variable, and still be correct. There is no difference with respect to program correctness. There is a difference with respect to succinctly expressing intent. – IInspectable Sep 11 '17 at 22:01

1 Answers1

1

If you think about it, it shouldn't really be much different. That is because when the computer sees a variable, it retrieves its memory adress and then the value. I have tried this code and the difference is so tiny (less than 3ms: BTW code ran in ~107 ms) that could be down to millions of other variables:

        //Just changed to non const
        const string s = "hello";

        string full = "";

        int k = 0;

        for(int i = 0; i < 10000; i++)
        {
            full += s;
        }

In general, const vs non const just comes down to this: will that value always stay the same: set it to const, else just leave it. Here is a great link about .NET optimization.

To sum up, You should only get into these tiny details the moment your code can't be optimized in any other way, and when that is the case, your code is just fine.

aPlutonicCoder
  • 116
  • 1
  • 11