- Home /
Static variables heavy for the iphone's CPU?
I use static variables quite a lot. I have a basic movement script for my main character and have lots of static vars (ok at the moment they are 8, I actually don't know if this is "lots") in it. The behavior of the other objects depends a lot on these static variables.
I was wondering if this is heavy for the iphone's cpu, as the device will have to switch from reading the various object's scripts to reading the static variables value from my basic script.
Answer by BerggreenDK · Dec 20, 2010 at 09:10 PM
If its 8 variables, I dont think you will be able to notice nor benchmark the difference. If you have a tilemap of 1024x1024 blocks in a global array perhaps. I dont know enough about how unity handles "statics".
Another thing is also to think of how often do you read/write to these variables? once a minute or everyframe?
Sometimes statics are both easier and faster than using functions to access the same variables.
My guess would be its faster this way at the moment, but without insight in your actual code, its hard to tell.
Thanks Berggreen, the static variables are read from and written to from inside "OnTriggerEnter" functions. So I understand it's better than if I had them inside "update" functions?
if you only have 8 I wouldnt really bother to think so much about them. There is something called a 80/20 rule. 80% of the actual gameloop(CPU) is run on 20% of the code. Just check where your variables are. Try to benchmark it if you think its slow. I would.
Answer by ChrisMasterton · Mar 21, 2012 at 12:07 AM
The fact that your variables are static won't make any difference. None.
Accessing variables through a function incurs the penalty of a function call. So in the simplest case direct access to the variable will be faster.
Answer by ChrisMasterton · Mar 21, 2012 at 12:07 AM
The fact that your variables are static won't make any difference. None.
Accessing variables through a function incurs the penalty of a function call. So in the simplest case direct access to the variable will be faster.
Your answer
Follow this Question
Related Questions
How to use a static var 2 Answers
My unity ios build is taking more than 2 hours 0 Answers
Can Apps ask for CPU power? 1 Answer
Static variables explanation. 2 Answers
guitexture a static var? 2 Answers