Search Unity

TextMeshPro - SetText() vs .text="someText" Speed Test

Discussion in 'Scripting' started by Maccyfin, Jul 6, 2020.

  1. Maccyfin

    Maccyfin

    Joined:
    Aug 19, 2012
    Posts:
    87
    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


    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:



    CODE TESTED

    Code (csharp):
    1.  
    2.     public float TMPSetTextTest()
    3.     {
    4.         TextMeshPro textmeshPro = GetComponent<TextMeshPro>();
    5.         if (null == textmeshPro)
    6.             textmeshPro = gameObject.AddComponent(typeof(TextMeshPro)) as TextMeshPro;
    7.         Stopwatch stopwatch = new Stopwatch();
    8.         stopwatch.Start();
    9.         Profiler.BeginSample("TMPSetTextVsDotTextProfiler.TMPSetTextTest");
    10.         textmeshPro.SetText("The first number is {0} and the 2nd is {1} and the 3rd is {3}.", 4, 5, 6);
    11.         Profiler.EndSample();
    12.         stopwatch.Stop();
    13.         return (float)stopwatch.Elapsed.TotalMilliseconds;
    14.     }
    15.     public float TMPDotTextEqualsTest()
    16.     {
    17.         TextMeshPro textmeshPro = GetComponent<TextMeshPro>();
    18.         if (null == textmeshPro)
    19.             textmeshPro = gameObject.AddComponent(typeof(TextMeshPro)) as TextMeshPro;
    20.         Stopwatch stopwatch = new Stopwatch();
    21.         stopwatch.Start();
    22.         Profiler.BeginSample("TMPSetTextVsDotTextProfiler.TMPDotTextEqualsTest");
    23.         textmeshPro.text = $"The first number is {4} and the 2nd is {5} and the 3rd is {6}.";
    24.         Profiler.EndSample();
    25.         stopwatch.Stop();
    26.         return (float)stopwatch.Elapsed.TotalMilliseconds;
    27.     }
    28.  
    HOWEVER

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





    And no garbage collection for either function:


    CODE TESTED

    Code (csharp):
    1.  
    2.     public float TMPSetTextNoConcatenationTest()
    3.     {
    4.         TextMeshPro textmeshPro = GetComponent<TextMeshPro>();
    5.         if (null == textmeshPro)
    6.             textmeshPro = gameObject.AddComponent(typeof(TextMeshPro)) as TextMeshPro;
    7.         Stopwatch stopwatch = new Stopwatch();
    8.         stopwatch.Start();
    9.         Profiler.BeginSample("TMPSetTextVsDotTextProfiler.TMPSetTextNoConcatenationTest");
    10.         textmeshPro.SetText("I am some text");
    11.         Profiler.EndSample();
    12.         stopwatch.Stop();
    13.         return (float)stopwatch.Elapsed.TotalMilliseconds;
    14.     }
    15.     public float TMPDotTextEqualsNoConcatenationTest()
    16.     {
    17.         TextMeshPro textmeshPro = GetComponent<TextMeshPro>();
    18.         if (null == textmeshPro)
    19.             textmeshPro = gameObject.AddComponent(typeof(TextMeshPro)) as TextMeshPro;
    20.         Stopwatch stopwatch = new Stopwatch();
    21.         stopwatch.Start();
    22.         Profiler.BeginSample("TMPSetTextVsDotTextProfiler.TMPDotTextEqualsNoConcatenationTest");
    23.         textmeshPro.text = $"I am some text";
    24.         Profiler.EndSample();
    25.         stopwatch.Stop();
    26.         return (float)stopwatch.Elapsed.TotalMilliseconds;
    27.     }
    28.  
    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
     
  2. mariaambr

    mariaambr

    Joined:
    Jul 15, 2020
    Posts:
    3
    ah, this was so helpful! thank you
     
    Zruena likes this.