Quantcast
Viewing latest article 24
Browse Latest Browse All 84

Does reading/changing Static variables constantly affect Performance or get Garbage Collected?

From the last question that i asked, @Bunny83 said: > `GUI.enabled` is just a state stored in an internal field. There is no overhead involved setting that state.
So `GUI.enabled` is a static bool, and i think static variables only have 1 reference, so when you access/change it somewhere, it will be accessed/changed for the others. So i'm mostly talking about Performance Improvement, does the last line improve performance at all? bool enabled = false; //Set it to (enabled) bool GUI.enabled = enabled; //Check if it isn't equal to (enabled) bool, so it isn't necessary to change it to the same value if //it is already (false), which it is equal to (enabled) bool, so the condition won't pass if(GUI.enabled != enabled)GUI.enabled = enabled;
Also `Event.current` is a static variable as well, so if i do: Event current = Event.current; if(current.button == 0) print("Left Mouse Button is pressed); Does the (current) variable that i just created affect performance? or get garbage collected?

`Event.current.button` is a public non static variable, so it has a reference to itself, so caching it into a variable is a good practice to increase performance?

Also `GetComponent()`isn't static as well, people suggest caching it, so it looks like `Event.current.button` should be cached when it gets used a lot!

And `List.Count` isn't static too, so i ran 2 different tests showing the difference between a cached `List.Count` and a non cached `List.Count` and it turned out that the cached `List.Count` is way too much faster than a non cached `List.Count`.

So can somebody explain it to me or confirm if it is true?
Thanks.

[EDIT] Just for your info, i have experience of about 3 years in Unity3D, so you don't need to explain the very basic stuff, i just talk about the Performance, so yeah..

Viewing latest article 24
Browse Latest Browse All 84

Trending Articles