0
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 ?

Patrick
  • 15,702
  • 1
  • 39
  • 39
user418836
  • 847
  • 3
  • 8
  • 19

2 Answers2

3

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 :

  • read the variable value into a register
  • increment the register
  • put back the value

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

Patrick
  • 15,702
  • 1
  • 39
  • 39
0

As far as i know there is no gradual difference between these two..

I have read a++ is faster than a = a+1;

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.

Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
  • I'm pretty sure this is not correct. There are typically operations that specifically increase or decrease a register by one. These are faster than the already fast "addition" and "subtraction"-operations of a CPU. The Flash VM probably does something similar, but probably not in the case of a property (only for local variables). – Jonatan Hedborg Jan 20 '12 at 11:52