Variable declaration performance on loops in Actionscript 3

放肆的年华 提交于 2020-01-03 12:05:50

问题


Despite all known blogs about this issue i always doubt some results and my personal tests shows that the well-said standard isn't the best.

Declaring variables inside the loop, to keep them close to its scope and make it faster to be reached by the method but allocating more memory or declaring outside the for scope to save memory allocation but increase processing to iterate in a distant instance.

My results shows that method B is faster(sometimes), i want to know the background around this.

results vary and im not a bit-brusher guru.

So what you guys think about it?

Method A

var object:Object = new Object();
var loop:int = 100000
for (var i:int = 0; i < loop; i++)
{
    object = new Object();
    object.foo = foo;
    object.bar = bar;
}

OR

Method B

var loop:int = 100000
for (var i:int = 0; i < loop; i++)
{
    var object:Object = new Object()
    object.foo = foo;
    object.bar = bar;
}

回答1:


tldr; they are semantically equivalent and perform identically.

There is only one variable called object in both cases presented. ActionScript, like JavaScript, "hoists" declarations. That is, var is really just a function-scoped annotation. This differs from C and Java where a new scope (and thus new variable) would have been created in the 2nd case.

There is no difference in AS, however. The engine effectively treats the 2nd code identical to the first. (That being said, I prefer to "keep the var close" to where it is used, while understanding it is not relevant to the scope and has no bearing on performance.)

See Action Script 3.0: Variables and, the Scope section in particular:

The scope of a variable is the area of your code where the variable can be accessed by a lexical reference... In ActionScript 3.0, variables are always assigned the scope of the function or class in which they are declared.

Happy coding.




回答2:


AS3 compiler moves all the variable declarations to the top of the method which is called variable hoisting. And the minimum scope for a variable is a complete method. Your method B is equivalent of the following:

var loop:int = 100000;
var i:int;
var object:Object;
for (i = 0; i < loop; i++) {
    object = new Object();
    object.foo = foo;
    object.bar = bar;
}

Note that it only moves the declaration up, not the associated assignment with this. This is the reason you can declare a variable after using it. For example try this code:

trace(a);
var a:int = 10;
trace(a);

This will compile. It's because this code is equivalent of:

var a:int;
trace(a);
a = 10;
trace(a);

This is also the reason that you will get a duplicate variable declaration warning with the following code:

for (var i:int = 0; i < m; i++) {

}
for (var i:int = 0; i < n; i++) { // i is already declared once

}

The concept of variable scope in AS3, JS is different from that of C, C++, Java etc.



来源:https://stackoverflow.com/questions/10743042/variable-declaration-performance-on-loops-in-actionscript-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!