在动作脚本循环变量声明性能3 [英] Variable declaration performance on loops in Actionscript 3

查看:132
本文介绍了在动作脚本循环变量声明性能3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尽管对这个问题的所有已知的博客,我总是怀疑一些成果和我个人的测试表明,良好的说,标准是不是最好的。

在循环内声明的变量,让他们接近其范围,使之更快地通过该方法来达到,但分配更多的内存或外部的范围内声明以节省内存分配,但会增加处理在一个遥远的实例来循环。

我的结果表明,方法B为快(有时),我想知道解决这个问题的背景。

结果各不相同,即时通讯不是一个位毛刷大师。

那么,你们认为呢?

方法A

  VAR对象:对象=新的对象();
VAR循环:INT = 100000
对于(VAR我:= 0; I<循环;我++)
{
    对象=新的对象();
    object.foo = foo的;
    object.bar =酒吧;
}
 

方法B

  VAR循环:INT = 100000
对于(VAR我:= 0; I<循环;我++)
{
    VAR对象:对象=新的对象()
    object.foo = foo的;
    object.bar =酒吧;
}
 

解决方案

tldr;它们是语义上等效,并执行相同的。

只有一片的一个的变量调用对象中的两个的情况下,presented。动作脚本,如JavaScript,吊的声明。也就是说, VAR 真的只是一个功能范围的注释的。这不同于C和Java,其中一个新的范畴(因而新的变量)的的在第二个情况下已经建立。

不过有一个在AS没有区别。该发动机的有效地治疗的第2 code等同于第一。 (话虽这么说,我preFER让企业 VAR 关闭的地方使用它,而理解它是不相关的范围和有对性能没有影响的。)

请参阅<一href="http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b90204-7f9d.html">Action脚本3.0:的变量,尤其范围部分:

  

变量的作用域是你的code,其中的变量可以通过词汇引用来访问......在ActionScript 3.0的区域,变量总是被分配函数或类的范围中,他们被声明。

快乐编码。

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;
}

解决方案

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.

这篇关于在动作脚本循环变量声明性能3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆