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
2
Question by Miriam · Jan 04, 2010 at 03:47 PM · guiperformanceoptimization

Static variables & performance

Hi, I'm trying to work out the most efficient location for this piece of code: inside the main script with changedTime as a private variable ; or pulled out into a separate script with changedTime as a static variable.

The main script is used on a lot of different objects and is performance critical. It uses changedTime to control GUI fades and animations. At any point in time there are usually 10-20 instances of the main script running.

Is it more efficient to access a static variable every OnGUI or to do these simple two lines of code every OnGUI and store a private variable? (I don't know how efficient static variables are).

private var oldTime : float; static var changedTime : float;

function OnGUI () {
changedTime = Time.time - oldTime; oldTime = Time.time; }

nb. for those curious - I'm using this instead of Time.deltaTime as Time.deltaTime does not seem to be accurate for OnGUI animations.

Thanks in advance!

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

2 Replies

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

Answer by duck · Jan 04, 2010 at 06:03 PM

My hunch is that it would be slightly faster to access a static variable (based on having timed other similar code to this degree, although not having actually tested the code you describe).

Having said that, I think it's worth noting that the difference between these two options is going to be incredibly small, and that it's probably much more important to identify whether this part of your code actually is a significant bottleneck in your game at all.

Optimising such a tiny piece of code to this degree kind of seems like it could only achieve a completely negligable difference in performance compared to many other aspects of your game such as draw calls, physics, object instantiation, and probably hundreds of other more costly functions used in your code.

If you really have optimised every other aspect of your game to such a degree that this is amongst the most significant items left, then my hat is off to you, sir/madam :-)

Comment
Add comment · Show 1 · 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 Miriam · Jan 05, 2010 at 09:32 AM 0
Share

Thanks! I will stick with the static variable.


Unfortunately I don't deserve the hat off. I'm reconstructing the game from scratch (we have a very rough prototype) and am optimizing as I go. Other scripts won't get this level of optimizing, but this one is used over and over throughout the game.

avatar image
3

Answer by dkoontz · Jan 05, 2010 at 04:52 AM

+1 for Duck. The overall cost of probably everything you're doing with regards to calculating and storing your time is most likely totally insignificant in the context of your project. One teeny tiny texture that needs to be loaded somewhere or allocating a new object(s) or any number of operatios that can be tucked away inside a function you call somewhere will be massivley more expensive than the simple computations you're worried about.

Now for raw numbers, setting a static variable once vs in 20 instances of your main behaviour (BTW, "main" as a name for something that has multiple instances is probably not the best choice), is most certainly faster as you're doing 1 / 20 the work. I'm with Duck on the access time as well, a static variable lookup has no indirection and thus should be just about as fast as you get for variable lookup. In the end though, I doubt you could even see the difference between static variable lookup and instance variable lookup unless you were running a test harness tens of thousands of times just to get to the 1 ms time difference range.

Comment
Add comment · Show 1 · 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 Miriam · Jan 05, 2010 at 09:37 AM 0
Share

Thanks!

nb. 'main' isn't the name of the actual script (that is 'GUI_Element').

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

No one has followed this question yet.

Related Questions

World canvas with screen space canvas performance mobile ? 0 Answers

HUD with a lot of elements, efficient way? 3 Answers

Are there any performance differences between a timer and using Invoke? 1 Answer

Reasonable heap alloc. per second and total ? 0 Answers

Sprite Alpha Performance 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