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 ZimonD · Jul 23, 2012 at 06:42 PM · variableloopdeclaration

Variable declaration, what's faster/more efficient?

Hello everyone,

I have a question concerning the declaration of variables, to explain, let's say we have a loop that draws buttons. Which of the following code is faster?

Method 1

 var a: Rect;
 for (var i = 0; i < 10; i++) {
     a.Set(10 * i, 10, 10, 10);
     // Some other code that actually draws the button
 }

or

Method 2

 for (var i = 0; i < 10; i++) {
     var a: Rect = Rect(10 * i, 10, 10, 10);
     // Some other code that actually draws the button
 }

I was thinking that because the first one declared a 'more global' variable, it is less fast. But in Method 2 a new variable needs to be declared in every iteration. Does anyone have a good answer for this? Is redefining a global variable faster than just creating a new one?

Thanks in advance, Z

Comment
Add comment · Show 3
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 fafase · Jul 23, 2012 at 06:44 PM 0
Share

What I know is when dealing with C++ is that the compiler will take the variable out during compilation (except if declared as volatile) because you are not altering the variable inside the loop.

avatar image ZimonD · Jul 23, 2012 at 06:57 PM 0
Share

And does taking out the variable increase performance (I assume you are referring to method 2)?

avatar image fafase · Jul 23, 2012 at 07:09 PM 0
Share

Actually I realized you are not doing the same thing in method 1 and 2. 1 is moving the same object 10 times, 2 is creating a new object at 10 different locations, so method 2 is more expensive since Set is just doing:

a.x = x; a.y = y; a.z = z;

Instantiate is doing more than that, getting parameter addresses, allocating memory, I think (not sure though maybe only on Destroy) returning a new map of the heap,...and so on.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Eric5h5 · Jul 23, 2012 at 07:12 PM

It depends on the kind of variable. In the case of Rect, it's a struct, and ideally you would want to avoid creating new structs if you don't have to. So the first method is better, but it would be better still to do this:

 var a = Rect(0, 10, 10, 10);
 for (var i = 0; i < 10; i++) {
     a.x = i*10;
     // stuff
 }

In any case it's very easy to run benchmarks (or use the profiler if you have Pro), so you can answer "which is faster" questions yourself with little effort, which is generally better than taking someone's word for it.

Comment
Add comment · Show 4 · 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 Bovine · Jul 23, 2012 at 07:58 PM 0
Share

Would creating new structs grow the stack? $$anonymous$$y understanding is that new value types are not created on the heap.

avatar image ZimonD · Jul 23, 2012 at 09:07 PM 0
Share

I can't afford Unity Pro (and it's Profiler utility that looks really handy). Would you $$anonymous$$d sharing a link or a bit of code on how you can benchmark? I used to get the CPU-time when testing console applications written in C, but I can't find something alike in Unity. (Google isn't helping either, as 'Unity' resembles 'Unit' and there is a rather large amount of bench marks for other platforms :s)

avatar image Bovine · Jul 23, 2012 at 09:51 PM 0
Share

I would start by following performance tips:

http://docs.unity3d.com/Documentation/$$anonymous$$anual/OptimizingGraphicsPerformance.html http://docs.unity3d.com/Documentation/ScriptReference/index.Performance_Optimization.html

Follow the many guidelines in there and worry about bottlenecks later down the line.

avatar image Eric5h5 · Jul 23, 2012 at 10:27 PM 0
Share

@Bovine: regardless, it's always faster to use an existing struct rather than creating a new one.

@ZimonD: you can use the .net Stopwatch class, or just Time.realtimeSinceStartup. $$anonymous$$ake sure you run enough code to get meaningful results, since ti$$anonymous$$g has a limited resolution, which often means looping millions or even billions of times, and try to do some real work so stuff doesn't just get optimized away by the compiler. (Although $$anonymous$$ono seems to be less clever than .net about that sort of thing anyway.)

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

7 People are following this question.

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

Related Questions

[help]How to move an object left and righ (looping) 6 Answers

How do I reset a variable? 2 Answers

Possible to use an array value as the name of a variable? 2 Answers

While something is reoccuring keep value the same 2 Answers

How would i make this code not lag the game 1 Answer


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