If the idea is to increment a variable and not care about the intermediate value, then pre-increment might be faster than post-increment. Here is why:
e-g incrementing an object variable
$o->aa++ vs ++$o->aa
The opcodes for post-increment:
3 POST_INC_OBJ ~1 !0, 'aa' 4 FREE ~1
has to save the incoming value (for later return) and do the actual increment. The returned value has to be "freed" even though it is not being used. Optimizers might look at these patterns and change them to pre-increment. If optimizers are not used, then this will end up wasting extra cycles.
The opcodes for pre-increment:
3 PRE_INC_OBJ !0, 'aa'
Nothing is returned and hence does not have to be freed.
e-g increment a variable
$i++ vs ++$i
8 POST_INC ~3 !1 9 FREE ~3
11 PRE_INC !1
The PHP opcodes can be easily dumped using tools like vld.
PHP: Pre-increment vs Post-increment
If the idea is to increment a variable and not care about the intermediate value, then pre-increment might be faster than post-increment. Here is why:
e-g incrementing an object variable
$o->aa++ vs ++$o->aa
The opcodes for post-increment:
3 POST_INC_OBJ ~1 !0, 'aa'4 FREE ~1
has to save the incoming value (for later return) and do the actual increment. The returned value has to be "freed" even though it is not being used. Optimizers might look at these patterns and change them to pre-increment. If optimizers are not used, then this will end up wasting extra cycles.
The opcodes for pre-increment:
3 PRE_INC_OBJ !0, 'aa'Nothing is returned and hence does not have to be freed.
e-g increment a variable
$i++ vs ++$i
8 POST_INC ~3 !19 FREE ~3
11 PRE_INC !1The PHP opcodes can be easily dumped using tools like vld.