- Home /
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!
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 :-)
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.
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.
Thanks!
nb. 'main' isn't the name of the actual script (that is 'GUI_Element').
Your answer
![](https://koobas.hobune.stream/wayback/20220613200010im_/https://answers.unity.com/themes/thub/images/avi.jpg)