This question has already been answered:
- Difference ++ i and i ++ in the for loop of 3 responses
What is better to use and what works rather?
$i++ , ++$i or $i+=1 ?
This question has already been answered:
What is better to use and what works rather?
$i++ , ++$i or $i+=1 ?
A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .
First, it is necessary to choose between them based on the required return value.
Secondly, if the return value is not used by us, good compilers would have to guess that this is all the same and make the same code.
Farther. In its original form, the instructions look something like this:
; ++i inc eax ; i++ mov ebx, eax inc eax ; i += 1 add eax, 1 Obviously, the first option can not be slower than the others. However, secondly, the compiler should understand that we do not use the copied value and throw out an extra instruction, and in the third, firstly, it is not clear whether addition is really less optimal, and secondly, the compiler can also understand this.
Conclusion: if there is no difference, write ++i , and the compiler will figure it out.
By the way, the use of similar variants of the assembly code already assumes that some optimizations have been applied. Because according to the ideology of most languages (apparently, apart from C and C ++), the increment instruction is divided into several: reading, addition and assignment.
In javascript, we can even make reading and writing take place with different objects:
var p = { x: 7 } var o = Object.create(p); ++ox; console.log(px); // по-прежнему 7 console.log(ox); // а вот тут уже своя 8 i += 1 instruction add eax, 1 - PashaPash ♦ function test(nominal){ var i = nominal; console.time(); while(i){ i--; } console.timeEnd(); i = nominal; console.time(); while(i){ --i; } console.timeEnd(); i = nominal; console.time(); while(i){ i = i - 1; } console.timeEnd(); } Got:
test(10000000) VM146:7 default: 50.000ms VM146:13 default: 31.000ms VM146:19 default: 25.000ms undefined test(10000000) VM146:7 default: 34.000ms VM146:13 default: 34.000ms VM146:19 default: 25.000ms undefined test(10000000) VM146:7 default: 49.000ms VM146:13 default: 32.000ms VM146:19 default: 24.000ms undefined test(10000000) VM146:7 default: 50.000ms VM146:13 default: 32.000ms VM146:19 default: 24.000ms undefined test(10000000) VM146:7 default: 34.000ms VM146:13 default: 33.000ms VM146:19 default: 24.000ms undefined test(10000000) VM146:7 default: 42.000ms VM146:13 default: 26.000ms VM146:19 default: 24.000ms We open the topic php
$nom = 10000000; function test($nominal){ $time = -microtime(true); $i = $nominal; while($i){ $i--; } $time += microtime(true); echo $time.' secs(i--), '; $time = -microtime(true); $i = $nominal; while($i){ --$i; } $time += microtime(true); echo $time.' secs(--i), '; $time = -microtime(true); $i = $nominal; while($i){ $i = $i - 1; } $time += microtime(true); echo $time.' secs(i-1), '; } $k = 10; while($k--){ echo '<br/>'; test($nom); echo '<br/>'; } >>>
1.3539199829102 secs(i--), 1.2611348628998 secs(--i), 1.3199110031128 secs(i-1), 1.1811420917511 secs(i--), 1.3495378494263 secs(--i), 1.3187339305878 secs(i-1), 1.1476590633392 secs(i--), 1.2748289108276 secs(--i), 1.3125870227814 secs(i-1), 1.1294658184052 secs(i--), 1.2662220001221 secs(--i), 1.3234519958496 secs(i-1), 1.1132800579071 secs(i--), 1.2643389701843 secs(--i), 1.3241758346558 secs(i-1), 1.1115510463715 secs(i--), 1.3130419254303 secs(--i), 1.3356699943542 secs(i-1), 1.1359589099884 secs(i--), 1.3663308620453 secs(--i), 1.3912291526794 secs(i-1), 1.2231979370117 secs(i--), 1.3729820251465 secs(--i), 1.4119091033936 secs(i-1), 1.3028910160065 secs(i--), 1.3418638706207 secs(--i), 1.3546941280365 secs(i-1), 1.1213128566742 secs(i--), 1.2591190338135 secs(--i), 1.347608089447 secs(i-1), Look here: http://php.net/manual/ru/language.operators.increment.php
<?php $i = 1; $j = 1; echo '$i = '. ++$i; echo '$j = '. $j++; ---- $i = 2; $j = 1; Here is the main difference.
что лучше использовать ? I only see the difference - Alexey ShimanskySource: https://ru.stackoverflow.com/questions/625580/
All Articles
$i = $i + 1? =) - user236014inc eaxis faster thanadd eax, 1. But no, we can not. - user236014