My friend (1st year cs + games undergrad student) and I (recently self taught amateur spaghetti creator) were discussing Global variables and the impact of scope on performance in c#.
After reading a Reddit post in which a certain automotive manufacturer’s source code was slated for having thousands of Global variables, I began to worry about my own classes and my potential overuse of global variables. As a self taught amateur, I have never had anyone around to tell me “You idiot, you have initialised 15 global variables in a 50 line class” etc…
Basically, I would like to know what is more efficient:
Note: “someArg” most likely will be a variable that changes frequently, up to once per Update cycle
// Pseudo-code:using library.whatever;public class ExampleClass : MonoBe - Pastebin.com (Initializing a global variable outside of the Update function)
// Pseudo-code:using library.whatever;public class ExampleClass : MonoBe - Pastebin.com (Initializing a local variable every update)
// Pseudo-code:using library.whatever;public class ExampleClass : MonoBe - Pastebin.com (I don’t even know what this is called… )
As someone who has yet to study computer science, I have no idea how c# actually handles this… Does it create and destroy local variables every cycle? Or does it intelligently store local variables in memory until they are no longer needed? What if it isn’t needed for a while, but then it is needed again later? I assume that the c# garbage collection works some magic here. I think that I should figure this out (with your help) before it becomes a stubborn problem…
TLDR: See the 3 pastebin links above - which one is the most efficient… And are Global variables bad enough to warrant changing my current style?