Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Question String.Length vs. (int) string > val ?

Discussion in 'Scripting' started by SeriouslyNot, Aug 5, 2020.

  1. SeriouslyNot

    SeriouslyNot

    Joined:
    Nov 24, 2017
    Posts:
    121
    I have int values that i want to display in TextMeshPro elements.

    I turned off AutoSize on all TMP elements to save performance, so i've to pre-calculate the fontSize values depending on the number of characters displayed.

    Whenever a value changes in any TMP, i check the character count and i just assign the pre-calculated value as fontSize for that count.

    To get the number of characters i'm using this:

    Code (CSharp):
    1. private void SetFontSize(string str)
    2. {
    3.         var length = str.Length;
    4.         if(length == 1)
    5.         {
    6.            a.fontSize = oneCharFontSize;
    7.         }
    8.         else if(length == 2)
    9.         {
    10.             a.fontSize = twoCharFontSize;
    11.         }
    12.         else if(intStr == 3)
    13.         {
    14.             a.fontSize = threeCharFontSize;
    15.         }
    16.        else if(intStr == 4)
    17.         {
    18.             a.fontSize = fourCharFontSize;
    19.         }
    20.         .....
    21. }

    Is it better this way or should i use:

    Code (CSharp):
    1. private void SetFontSize(string str)
    2. {
    3.         int intStr = (int) str;
    4.         a.fontSize = oneCharFontSize;
    5.         if(intStr > 9)
    6.         {
    7.            a.fontSize = twoCharFontSize;
    8.         }
    9.         else if(intStr > 99)
    10.         {
    11.             a.fontSize = threeCharFontSize;
    12.         }
    13.         else if(intStr > 999)
    14.         {
    15.             a.fontSize = fourCharFontSize;
    16.         }
    17.         .....
    18. }

    Which one is less expensive if i'm doing this A LOT ?
     
    Last edited: Aug 5, 2020
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,921
    Why not put all of your precalculated font sizes in an array, and then just do this:

    Code (CSharp):
    1. int[] fontSizes;
    2.  
    3. private void SetFontSize(string str)
    4. {
    5.     var length = str.Length;
    6.     a.fontSize = fontSizes[length - 1];
    7. }
     
  3. SeriouslyNot

    SeriouslyNot

    Joined:
    Nov 24, 2017
    Posts:
    121
    @PraetorBlue
    That's what i'm doing actually i don't know why i wrote the if else statements, but my question is about the .Length function:
    is it expensive to use? should i just cast to int and check if larger than 9 and 99 and 999 respectively?

    Keep in mind that i'm doing this like 40 times per second.
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,921
    Length is not expensive to use. Parsing the string to an int is definitely slower. I don't think it's like strlen in C where it traverses the whole string to find a null terminator. Even if it was, parsing would require reading the whole string too and doing a lot more work than just counting the characters.
     
    Suddoha and SeriouslyNot like this.
  5. SeriouslyNot

    SeriouslyNot

    Joined:
    Nov 24, 2017
    Posts:
    121
    Well if this is the case then i'll stay with .Length

    Thank you man :)