Sign up Calendar Latest Topics
 
 
 


Reply
  Author   Comment  
Martin

Avatar / Picture

Administrator
Registered:
Posts: 2,230
Reply with quote  #1 
Hey all. Today I looked into which string concatenate method works best in regards to speed. The methods I tested were:

- String + operator: string1 + string 2;
- String interpolation: $"{string1}{string2};
- String.Concat(): string.Concat(string1, string2);
- String format: System.String.Format("{0}{1}", string1, string2);

RESULTS

StringConcatTests_July3rd2020.jpg 

SPEED (fastest at the top)

1) String.Concat()
2) String + operator
3) String interpolation
4) System.String.Format


CODE
Here's the full code I tested along with the timer methods:

    public float StringPlusOperatorTest()
    {
        Stopwatch stopwatch = new Stopwatch();
        stopwatch.Start();
        string sWord1 = "One";
        string sWord2 = "Two";
        string sWords = sWord1 + sWord2;
        stopwatch.Stop();
        return (float)stopwatch.Elapsed.TotalMilliseconds;
    }
    public float StringInterpolationTest()
    {
        Stopwatch stopwatch = new Stopwatch();
        stopwatch.Start();
        string sWord1 = "One";
        string sWord2 = "Two";
        string sWords = $"{sWord1}{sWord2}";
        stopwatch.Stop();
        return (float)stopwatch.Elapsed.TotalMilliseconds;
    }
    public float StringDotConcatTest()
    {
        Stopwatch stopwatch = new Stopwatch();
        stopwatch.Start();
        string sWord1 = "One";
        string sWord2 = "Two";
        string sWords = string.Concat(sWord1, sWord2);
        stopwatch.Stop();
        return (float)stopwatch.Elapsed.TotalMilliseconds;
    }
    public float StringDotFormatTest()
    {
        Stopwatch stopwatch = new Stopwatch();
        stopwatch.Start();
        string sWord1 = "One";
        string sWord2 = "Two";
        string sWords = System.String.Format("{0}{1}", sWord1, sWord2);
        stopwatch.Stop();
        return (float)stopwatch.Elapsed.TotalMilliseconds;
    }


NOTES / CONCLUSION

Based on the results above it would seem that the difference between the + operator, string interpolation and string.Concat() is very small and probably won't make that much difference in a full game code base. However String.Format takes three times as much time as string interpolation.

The results above also don't take into account garbage collection and its impact on the test. After speaking with some of the other forum members, it's something I need to take into account when doing these benchmarks. 

Any feedback as always is great guys. I hope these are useful, or interesting at the least.

Happy Developing 😉
Martin

0
Martin

Avatar / Picture

Administrator
Registered:
Posts: 2,230
Reply with quote  #2 
To add a point on the GC (Garbage Collection) front. It also looks like each of the 4 methods have the same amount of memory allocated. And so no memory benefit of using one above the other.

StringConcatTests-2_July3rd2020.jpg

By the way, does anyone know if we can get the GC Alloc value via script for a specific chunk of code?

0
Previous Topic | Next Topic
Print
Reply

Quick Navigation:

Easily create a Forum Website with Website Toolbox.