Question by 
               Ossorum · Nov 08, 2016 at 06:56 PM · 
                unity5optimizationportingspeed issues  
              
 
              Why is my C# code super slow!?
I have a maze generation code that I originally coded in python and ported it to JavaScript before I porting to unity (C#).
But when I run the generator it takes like 30 seconds, when the Python & JS versions ran almost instantly?!
Why is this, and can it be fixed?
Here is my code:
 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 public class GenerateMaze : MonoBehaviour {
     public static string[,] generate(int[] size, out int startHeight, out int finishHeight, out List<int[]> order){//order is for debug (remove all occerences later)
         order = new List<int[]>();
         //{ create starting vars
         int[][] relativeAdjacent = new int[][] {
             new int[]{0, 1},
             new int[]{ 0, -1},
             new int[]{ 1, 0},
             new int[]{ -1, 0}
         };
         string[,] maze = new string[size[0],size[1]];
         for (int i = 0; i < size[0]; i++) {
             for (int j = 0; j < size[1]; j++) {
                 maze[i,j] = "wall";
             }
         }
         int[] startPos = new int[2] {Random.Range(1, size[0]-1), Random.Range(1, size[1]-1)};
         maze[startPos[0],startPos[1]] = "floor";
         order.Add(startPos);
         //{ create toTestPoses and give initial values
         int numAdjacentToLastPos = 0;
         List<int[]> toTestPoses = new List<int[]>();
             
         if (true) {//to allow "add" to be dedefined
             bool add;
             foreach (int[] dir in relativeAdjacent) {
                 int[] checkPos = new int[2] { startPos[0]+dir[0], startPos[1]+dir[1] };
                 if (checkPos[0] < 1 || checkPos[0] > size[0] - 2) {
                     add = false;
                 } else if (checkPos[1] < 1 || checkPos[1] > size[1] - 2) {
                     add = false;
                 } else {
                     add = true;
                 }
                 if (add) {
                     toTestPoses.Add(new int[2] { checkPos[0],checkPos[1] });
                     numAdjacentToLastPos++;
                 }
             }
         }
         //}
         //}{ main part
         while (toTestPoses.Count > 0) {
             int indexOfTestPos;
             if (numAdjacentToLastPos > 0) {
                 indexOfTestPos = toTestPoses.Count-Random.Range(1, numAdjacentToLastPos+1);
                 numAdjacentToLastPos--;
             } else {
                 indexOfTestPos = Random.Range(0, toTestPoses.Count);
             }
             int[] testPos = toTestPoses[indexOfTestPos];
             toTestPoses.RemoveAt(indexOfTestPos);
             //---calculate numOfAdjacentFloors to know whether to place this pos
             int numOfAdjacentFloors = 0;
             foreach (int[] dir in relativeAdjacent) {
                 int[] adjacent = new int[2] { testPos[0] + dir[0],  testPos[1] + dir[1] };
                 if (maze[adjacent[0],adjacent[1]] == "floor") {
                     numOfAdjacentFloors++;
                 }
             }
             //---
             if (numOfAdjacentFloors == 1) {//good to add floor
                 order.Add(testPos);
                 numAdjacentToLastPos = 0;//reset because we now have a new pos
                 maze[testPos[0],testPos[1]] = "floor";
                 bool add;
                 foreach (int[] dir in relativeAdjacent) {
                     int[] checkPos = new int[2] { testPos[0]+dir[0], testPos[1]+dir[1] };
                     if (checkPos[0] < 1 || checkPos[0] > size[0] - 2) {
                         add = false;
                     } else if (checkPos[1] < 1 || checkPos[1] > size[1] - 2) {
                         add = false;
                     } else if (maze[checkPos[0],checkPos[1]] == "floor" ) {
                         add = false;
                     } else {
                         add = true;
                     }
                     if (add) {
                         toTestPoses.Add(checkPos);
                         numAdjacentToLastPos++;
                     }
                 }
             }
             Debug.Log(toTestPoses); 
             
         }
         startHeight = 0;
         if (true) {//so found can be dedefined
             bool found = false;
             while (!found) {
                 startHeight = Random.Range(1, size[1]-1);
                 if (maze[1, startHeight] == "floor") {
                     maze[0, startHeight] = "floor";
                     found = true;
                 }
             }
         }
         finishHeight = 0;
         if (true) {//so found can be dedefined
             bool found = false;
             while (!found) {
                 finishHeight = Random.Range(1, size[1]-1);
                 if (maze[size[0]-2, finishHeight] == "floor") {
                     maze[size[0]-1, finishHeight] = "floor";
                     found = true;
                 }
             }
         }
         return maze;
         //}
     }
 }
               Comment
              
 
               
              Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                