Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
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
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

84 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity's / C# limitations with high number of object instances? 1 Answer

Force a uGUI scrollbar/scrollrect to stay at the bottom when the scrollrect is expanded 0 Answers

third person camera zoom in and zoom out 0 Answers

How to calculate velocity for Basketball 0 Answers

question about optimization script damage 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges