Home  >  Article  >  Backend Development  >  Comparison of the differences between ++i and i++ in php_PHP tutorial

Comparison of the differences between ++i and i++ in php_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:14:191160browse

++i and i++ are used in many programming to add +1 to a variable, but there is a problem of sequence. Let me introduce some differences between them in operation.


1. Usage of ++i (take a=++i, i=2 as an example)

First add 1 to the i value (that is, i=i+1), and then assign it to the variable a (that is, a=i),

Then the final a value is equal to 3 and the i value is equal to 3.

So a=++i is equivalent to i=i+1 , a=i

2. Usage of i++ (take a=i++, i=2 as an example)

First assign the i value to the variable a (that is, a=i), then add 1 to the i value (that is, i=i+1),

Then the final a value is equal to 2 and the i value is equal to 3.

So a=i++ is equivalent to a=i , i=i+1

3. ++i and i++

a=++i is equivalent to i++ , a=i

a=i++ is equivalent to a=i , i++

4. When ++i and i++ are used alone, they are equivalent to i=i+1

If assigned to a new variable, ++i first adds 1 to the i value, and i++ first assigns i to the new variable.

Performance optimization

while(++$j < 10000) ;
The code is as follows
 代码如下 复制代码
方式一:
 
$begin = time();
$i = 0;
while(++$i < 10000)
{
  $j = 0;
  while(++$j < 10000)
    ;
  ;
}
$end = time();
 
时间 : 16s
 
方式二:
 
$begin = time();
$i = 0;
while($i < 10000)
{
  $j = 0;
  while($j < 10000)
    ++$j;
  ++$i;
}
$end = time();
 
时间:13s
 
方式三:
 
$begin = time();
$i = 0;
while($i < 10000)
{
  $j = 0;
  while($j < 10000)
    $j++;
  $i++;
}
$end = time();
 
时间:15s
 
方式四:
 
$begin = time();
$i = 0;
while($i++ < 10000)
{
  $j = 0;
  while($j++ < 10000)
    ;
  ;
}
$end = time();
时间:13s
Copy code

Method 1:

$begin = time();

$i = 0;

while(++$i < 10000)

{

$j = 0;
; }

$end = time();

Method 2: $begin = time(); $i = 0; while($i < 10000) { $j = 0; while($j < 10000) ++$j; ++$i;
} $end = time();
Time:13s
Method 3: $begin = time(); $i = 0; while($i < 10000) { $j = 0; while($j < 10000) $j++; $i++; } $end = time(); Time:15s Method 4: $begin = time(); $i = 0; while($i++ < 10000) { $j = 0; while($j++ < 10000) ; ; } $end = time(); Time:13s Compare the first method and the second method, because in PHP, what is ultimately executed is OPCODE, each line of opline has two operands. For the operands, there are generally three types of access methods, temporary variables, variables, and compile-time variables. These three types of variables Among them, the third type with the fastest access is compiler variables. During the execution of OpCode, a variable plus a first-level reference will be stored in a hash structure, used to speed up access. http://www.bkjia.com/PHPjc/629024.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/629024.htmlTechArticle++i and i++ are used in many programming, and the +1 operation is added to the variable, but there is a sequencing problem , let me introduce some differences between them in operation. 1. Usage of ++i (with a=++...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn