This question has already been answered:

What is better to use and what works rather?

$i++ , ++$i or $i+=1 ?

Reported as a duplicate by members of Pavel Mayorov , Kromster , PashaPash 9 Feb '17 at 13:22 .

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 .

  • one
    so javascript or php? - Grundy
  • or $i = $i + 1 ? =) - user236014
  • I would like to know in both - vov4ok
  • 7
    Definitely, we can only say that inc eax is faster than add eax, 1 . But no, we can not. - user236014
  • eight
    we want to say that if the difference in speed of these two operators is critical, then it is necessary to write in assembly language - Ipatiev

3 answers 3

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 

  • You can delegate a compiler, in this case it may even be necessary, but the compiler cannot refine all the code, you still need to know how and what works :) - user207618
  • @Other, I added another point in response. - Qwertiy
  • Yes, fun fact. But why ignore the fact that they read and write to different places? Like read the prototype and write to the same. The error, of course, is not an option, the property is. - user207618
  • @Other, because we always write to the object. No cases when it is not. - Qwertiy
  • one
    @Qwertiy no sane compiler makes 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), 
  • PHP topic not disclosed - PashaPash
  • @PashaPash, do you think there will be other results in pehape with such tests? - user207618
  • @PashaPash, revealed - SLy_huh
  • @Other, surprisingly, I can not say that they are clearly similar. - SLy_huh
  • My chrome does not agree with you. The last one is the slowest: i.stack.imgur.com/B1VYw.png (of course, the first run does not count - there the function was compiled). - Qwertiy

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.

  • one
    there was a question of what the difference is visible, and in performance - Alexey Shimansky
  • one
    the person also asked "what is better to use." - Kirill Korushkin
  • one
    and where is the answer to the question что лучше использовать ? I only see the difference - Alexey Shimansky
  • About the difference I know - vov4ok
  • even somehow ashamed already that I answered here. - Kirill Korushkin