Sign up Calendar Latest Topics
 
 
 


Reply
  Author   Comment  
Martin

Avatar / Picture

Administrator
Registered:
Posts: 2,230
Reply with quote  #1 
Hey all, hope everyone is enjoying their week of dev'ing 😉
I recently found out from some guys on the forum that the TextMeshPro plugin has a .SetText() method which avoids unnecessary garbage collection when concatenation strings. So I thought I'd test this out.

RESULTS

TMP.SetTextVsDotTextEquals.jpg 
SPEED

Above you can see that using TextmeshPro.SetText("text") is faster than doing textmeshPro.text = "text".

And below it shows that TextmeshPro.SetText("text") also results in lower garbage collection:

TMP.SetTextVsDotTextEquals - 2.jpg

CODE TESTED


    public float TMPSetTextTest()
    {
        TextMeshPro textmeshPro = GetComponent<TextMeshPro>();
        if (null == textmeshPro)
            textmeshPro = gameObject.AddComponent(typeof(TextMeshPro)) as TextMeshPro;
        Stopwatch stopwatch = new Stopwatch();
        stopwatch.Start();
        Profiler.BeginSample("TMPSetTextVsDotTextProfiler.TMPSetTextTest");
        textmeshPro.SetText("The first number is {0} and the 2nd is {1} and the 3rd is {3}.", 4, 5, 6);
        Profiler.EndSample();
        stopwatch.Stop();
        return (float)stopwatch.Elapsed.TotalMilliseconds;
    }
    public float TMPDotTextEqualsTest()
    {
        TextMeshPro textmeshPro = GetComponent<TextMeshPro>();
        if (null == textmeshPro)
            textmeshPro = gameObject.AddComponent(typeof(TextMeshPro)) as TextMeshPro;
        Stopwatch stopwatch = new Stopwatch();
        stopwatch.Start();
        Profiler.BeginSample("TMPSetTextVsDotTextProfiler.TMPDotTextEqualsTest");
        textmeshPro.text = $"The first number is {4} and the 2nd is {5} and the 3rd is {6}.";
        Profiler.EndSample();
        stopwatch.Stop();
        return (float)stopwatch.Elapsed.TotalMilliseconds;
    }

HOWEVER

When doing the same test but without concatenating strings, it would appear that textmeshPro.text = "some text", is faster. 

TMP.SetTextVsDotTextEquals - no concatenation - 1.jpg 
And no garbage collection for either function:

TMP.SetTextVsDotTextEquals - no concatenation - 2.jpg 
CODE TESTED


    public float TMPSetTextNoConcatenationTest()
    {
        TextMeshPro textmeshPro = GetComponent<TextMeshPro>();
        if (null == textmeshPro)
            textmeshPro = gameObject.AddComponent(typeof(TextMeshPro)) as TextMeshPro;
        Stopwatch stopwatch = new Stopwatch();
        stopwatch.Start();
        Profiler.BeginSample("TMPSetTextVsDotTextProfiler.TMPSetTextNoConcatenationTest");
        textmeshPro.SetText("I am some text");
        Profiler.EndSample();
        stopwatch.Stop();
        return (float)stopwatch.Elapsed.TotalMilliseconds;
    }
    public float TMPDotTextEqualsNoConcatenationTest()
    {
        TextMeshPro textmeshPro = GetComponent<TextMeshPro>();
        if (null == textmeshPro)
            textmeshPro = gameObject.AddComponent(typeof(TextMeshPro)) as TextMeshPro;
        Stopwatch stopwatch = new Stopwatch();
        stopwatch.Start();
        Profiler.BeginSample("TMPSetTextVsDotTextProfiler.TMPDotTextEqualsNoConcatenationTest");
        textmeshPro.text = $"I am some text";
        Profiler.EndSample();
        stopwatch.Stop();
        return (float)stopwatch.Elapsed.TotalMilliseconds;
    }


NOTES / CONCLUSION

If I was to choose one I would say use TextMeshPro.SetText() as its faster overall. It's slightly slower when not concatenating a string, but not by much. The major point is that it leads to lower garbage collection and so fewer GC spikes throughout the life of your game.

It'll be good to hear what others think about this. As always let me know if I've missed something in this test and I'll make adjustments, thanks for any input.

Happy Developing 😉
Martin

0
Previous Topic | Next Topic
Print
Reply

Quick Navigation:

Easily create a Forum Website with Website Toolbox.