Actionscript 3.0 getter setter增量
private var _variable:int;
public function set variable(val:int):void{
_variable = val;
}
public function get variable():int{
return _variable
}
现在,如果我必须增加变量......哪一种是更优化的做法?
__instance.variable++;
或者
__instance.variable = __instance.variable + 1;
问这个问题的原因是,我读过 a++ is fast than a = a+1; 。即使使用 getter 和 setter 时,同样的原则也适用吗?
private var _variable:int;
public function set variable(val:int):void{
_variable = val;
}
public function get variable():int{
return _variable
}
Now if I have to increment the variable... which one is more optimized way of doing ?
__instance.variable++;
or
__instance.variable = __instance.variable + 1;
The reason for asking this question is, I have read a++ is faster than a = a+1;. Would the same principle apply even when using getters and setters ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通常不会,它们会以相同的方式进行转换,因为虚拟机内没有特殊的操作码来执行此操作,虚拟机必须执行以下操作:
,现在它更短了,与第二种方式相比,编写 __instance.variable++ 更不容易出错。
相反,当您执行
var++
增加局部变量时,它存在一个特殊操作(inclocal或inclocal_i(i代表整数)),它将直接增加寄存器的值,因此它可以稍微快一些。以下是 AVM2 操作码的示例列表:
http://www.anotherbigidea.com/javaswf/avm2/AVM2Instructions.html
No normally they will be translated the same way because there is no special opcode within the VM to do this operation, the VM will have to do these operations :
now it's shorter and less error prone to write
__instance.variable++
than the second way.In contrary when you increment a local variable doing
var++
it exists a special operation (inclocal or inclocal_i (i stand for integer) ) that will directly increment the value of the register so it can be slightly faster.Here a list for example of the AVM2 opcode :
http://www.anotherbigidea.com/javaswf/avm2/AVM2Instructions.html
据我所知,这两者之间没有逐渐的区别。
其实你的这个说法是一个悖论。
因为编译器(在本例中为 C 编译器)和解释器将 a++ 视为 a=a+1 ,所以即使您编写 a++.它不会产生巨大的影响。
As far as i know there is no gradual difference between these two..
Actually this statement of yours is a Paradox.
Because compilers(C compiler in this case) and interprets consider a++ as a=a+1 , so even though you write a++. Its not going to make a huge difference.