Search Unity

  1. Calling all beginners! Join the FPS Beginners Mods Challenge until December 13.
    Dismiss Notice
  2. It's Cyber Week at the Asset Store!
    Dismiss Notice

Split String into Multidimensional Array

Discussion in 'Scripting' started by jakob_wit, Mar 20, 2014.

  1. jakob_wit


    Mar 20, 2014
    Do you know if I can optimize my code with regex or simplify the code.


    Code (csharp):
    2.     private bool Load (string file)
    3.     {
    4.         try
    5.         {
    6.             string line = null;
    7.                      int i = 0;
    9.             StreamReader sr = new StreamReader(Application.dataPath+"/"+wordsDir+"/"+file);
    11.             using(sr){
    13.                 string[,] wordsArray = new string[5,4];
    15.                 while((line = sr.ReadLine()) != null)
    16.                 {
    17.                     string[]tempArray2 = line.Split(',');
    19.                     for(int j=0; j<4; j++)
    20.                     {
    21.                        wordsArray[i,j] = tempArray2[j];
    22.                        Debug.Log("OutputFiled"+i+"/"+j+":"+wordsArray[i,j]);
    23.                     }
    24.                                    i++;
    25.                 }
    26.             }
    28.          return true;
    29.         }
    30.         catch(IOException e)
    31.         {
    32.             Debug.LogError(e.Message);
    33.             return false;
    34.         }
    35.     }
    Last edited: Mar 20, 2014
  2. lordofduct


    Oct 3, 2011
    well your array is static in size and never goes larger than length 5 in the 1st dimension. So you could add a check at the end of the while loop that tests if i > 4, and break the loop. This is actually more because it's a potential error waiting to happen if the file that is loaded has more lines added to it.

    If you wanted a dynamically sized array though that supported how ever many lines, and commas per line, that are in the file. Well your code will have to change.

    Otherwise though, optimization, this a Load call, and the code is efficient enough, don't worry about it.

  3. Kirlim


    Aug 6, 2012
    Is the execution so noticeably slow that you need to optimize this method? Is it really this method that is the proven culprit in making everything halt?

    If 'no' for the first question, your time is better used implementing new features than optimizing something into the microseconds level, where no one might ever notice that the code is efficient there. Maybe not even you. Bigger problem if this is not slow and happens once per every execution of the game.

    If 'dunno' for the second question, you need to find a way to profile your code and find the hotspot there. Programmers sucks so much in intuition of bottlenecks as we suck in drawing disney animations. There are some tricks to profile code on unity free.

    Making short, solve a problem when it is a problem. Otherwise, make the game better with new cool features and lots of eye-candies.

    Now onto the question. Regex needs processing, where directly breaking the string every specific character is only a comparison. Breaking the string into many results can be considered the same execution time (unless you execute this more times per seconds than there are stars on the sky, then regex might be faster because of lesse garbage collecting - my guess only, I have no analytic data to prove this). So as of now, it doesnt matter. Maybe if you start using syntax rules in the future, then regex will be more useful - even though there are probably better solutions like json, xml, lua, etc

    Second question: simplifying the code? Stop using magic numbers (like j < 4). Numbers come from some logic, and usually have some meaning to them. If they wont change, make a constant so that the code reader (including you, 3 months from now) can quickly know what is happening in the code. You will forget what your code does and eventually a curse will make you need to fix some bug there.

    Last note: avoid try/catch if deploying for mobile. Dunno if this is still true.