Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by MountDoomTeam · Oct 09, 2012 at 01:02 PM · functionvariablesglobalfaster

function is faster when accessing function variables not global ones?

i have a function FindThings() that compares lots of Vector3's found with findWithTag to one global var PlayerLocation:Vector3 from the top of the script

it seems that if FindThings() calls PlayerLocation multiple times from the global variables, it goes slower than if PlayerLocation is sent to the function as a function variable:

 FindThings(PlayerLocation); 
 {
 Do various PlayerLocation comparisons;
 }

is faster than

 var PlayerLocation:Vector3;
 
 FindThings()
 {
 Do various PlayerLocation comparisons;
 }


EDIT: this function is going very slowly, it was even slower when location was a global variable. i have another version of the same function that goes 100 times faster. it's odd.

 function delAdjoining(location: Vector3)//Dell all walls surrounding a space if revisited prior to rebuilding
 {
         for (var j:int = 0; j < neighbours.length; j ++)
         {
             if ( neighbours[j] == location )
             {    
                 var dwalls = GameObject.FindGameObjectsWithTag ("wall");
                 for (var dwall in dwalls)
                 {            
                     if(dwall.transform.position ==  (location + stp1*.5*sizeMult)){Destroy(dwall);}
                     if(dwall.transform.position ==  (location + stp2*.5*sizeMult)){Destroy(dwall);}
                     if(dwall.transform.position ==  (location + stp3*.5*sizeMult)){Destroy(dwall);}
                     if(dwall.transform.position ==  (location + stp4*.5*sizeMult)){Destroy(dwall);}
                     if(dwall.transform.position ==  (location + stp5*.5*sizeMult)){Destroy(dwall);}
                     if(dwall.transform.position ==  (location + stp6*.5*sizeMult)){Destroy(dwall);}
                     print("erased adjoining");            
                 }
                 break;
             }
         }
 }
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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Eric5h5 · Oct 09, 2012 at 01:22 PM

Yes, although the difference is very small. You would generally prefer to keep variables local primarily because of better script organization; the speed benefit is just a bonus since it will not be measurable in most cases.

Comment
Add comment · Show 2 · Share
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
avatar image MountDoomTeam · Oct 09, 2012 at 02:45 PM 0
Share

I have a function that takes about 1-2 seconds to process on an old laptop, and i changed it to a function variable and it seemed to take about 1/2 a second to process.

I also have another version of the same function that is written almost exactly the same, but takes about 1/100th of a second. i updated my original question with the slow code

avatar image Eric5h5 · Oct 09, 2012 at 06:14 PM 0
Share

You'd have to loop millions of times to be able to tell the difference in the profiler, never $$anonymous$$d a stopwatch, so I expect you have some other difference as well. One thing you can do to speed up the function (which will make far more of a difference) is to declare a variable, "var dwallTransform = dwall.transform;" and use that ins$$anonymous$$d of dwall.transform, and use "else if" ins$$anonymous$$d of "if", so it will skip evaluating those conditions where it doesn't need to.

avatar image
1

Answer by BPPHarv · Oct 09, 2012 at 07:26 PM

That really slow code you posted as your edit is slow because you're doing GameObject.FindGameObjectsWithTag ("wall"); repeatedly.

FindGameObjectWithTag is slow. To get a true sense of the speed gain cache the result of FindGameObjectWithTag in an array.

Comment
Add comment · Share
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
avatar image
1

Answer by hvilela · Oct 09, 2012 at 06:49 PM

Move your GameObject.FindGameObjectsWithTag outside the loop, cause it's slowly.

 function delAdjoining(location: Vector3)//Dell all walls surrounding a space if revisited prior to rebuilding
 {
        var dwalls = GameObject.FindGameObjectsWithTag ("wall");
        for (var j:int = 0; j < neighbours.length; j ++)
        {
          if ( neighbours[j] == location )
          {    
           for (var dwall in dwalls)
           {      
               if(dwall.transform.position ==  (location + stp1*.5*sizeMult)){Destroy(dwall);}
               if(dwall.transform.position ==  (location + stp2*.5*sizeMult)){Destroy(dwall);}
               if(dwall.transform.position ==  (location + stp3*.5*sizeMult)){Destroy(dwall);}
               if(dwall.transform.position ==  (location + stp4*.5*sizeMult)){Destroy(dwall);}
               if(dwall.transform.position ==  (location + stp5*.5*sizeMult)){Destroy(dwall);}
               if(dwall.transform.position ==  (location + stp6*.5*sizeMult)){Destroy(dwall);}
                    print("erased adjoining");         
           }
           break;
          }
        }
 }
Comment
Add comment · Share
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

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

12 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

Related Questions

Function is not updating the variable 1 Answer

Accessing variables from seperate scripts 1 Answer

Custom Global Functions 1 Answer

How can you hide variables within a region in a c# script? 1 Answer

How i "import" variables? 2 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