- Home /
Variable declaration, what's faster/more efficient?
Hello everyone,
I have a question concerning the declaration of variables, to explain, let's say we have a loop that draws buttons. Which of the following code is faster?
Method 1
var a: Rect;
for (var i = 0; i < 10; i++) {
a.Set(10 * i, 10, 10, 10);
// Some other code that actually draws the button
}
or
Method 2
for (var i = 0; i < 10; i++) {
var a: Rect = Rect(10 * i, 10, 10, 10);
// Some other code that actually draws the button
}
I was thinking that because the first one declared a 'more global' variable, it is less fast. But in Method 2 a new variable needs to be declared in every iteration. Does anyone have a good answer for this? Is redefining a global variable faster than just creating a new one?
Thanks in advance, Z
What I know is when dealing with C++ is that the compiler will take the variable out during compilation (except if declared as volatile) because you are not altering the variable inside the loop.
And does taking out the variable increase performance (I assume you are referring to method 2)?
Actually I realized you are not doing the same thing in method 1 and 2. 1 is moving the same object 10 times, 2 is creating a new object at 10 different locations, so method 2 is more expensive since Set is just doing:
a.x = x; a.y = y; a.z = z;
Instantiate is doing more than that, getting parameter addresses, allocating memory, I think (not sure though maybe only on Destroy) returning a new map of the heap,...and so on.
Answer by Eric5h5 · Jul 23, 2012 at 07:12 PM
It depends on the kind of variable. In the case of Rect, it's a struct, and ideally you would want to avoid creating new structs if you don't have to. So the first method is better, but it would be better still to do this:
var a = Rect(0, 10, 10, 10);
for (var i = 0; i < 10; i++) {
a.x = i*10;
// stuff
}
In any case it's very easy to run benchmarks (or use the profiler if you have Pro), so you can answer "which is faster" questions yourself with little effort, which is generally better than taking someone's word for it.
Would creating new structs grow the stack? $$anonymous$$y understanding is that new value types are not created on the heap.
I can't afford Unity Pro (and it's Profiler utility that looks really handy). Would you $$anonymous$$d sharing a link or a bit of code on how you can benchmark? I used to get the CPU-time when testing console applications written in C, but I can't find something alike in Unity. (Google isn't helping either, as 'Unity' resembles 'Unit' and there is a rather large amount of bench marks for other platforms :s)
I would start by following performance tips:
http://docs.unity3d.com/Documentation/$$anonymous$$anual/OptimizingGraphicsPerformance.html http://docs.unity3d.com/Documentation/ScriptReference/index.Performance_Optimization.html
Follow the many guidelines in there and worry about bottlenecks later down the line.
@Bovine: regardless, it's always faster to use an existing struct rather than creating a new one.
@ZimonD: you can use the .net Stopwatch class, or just Time.realtimeSinceStartup. $$anonymous$$ake sure you run enough code to get meaningful results, since ti$$anonymous$$g has a limited resolution, which often means looping millions or even billions of times, and try to do some real work so stuff doesn't just get optimized away by the compiler. (Although $$anonymous$$ono seems to be less clever than .net about that sort of thing anyway.)
Your answer
