Comparison of ++ I and I ++ in php

Source: Internet
Author: User
In many programming languages, ++ I and I ++ add + 1 operations to variables, but there are successive problems. let's introduce the differences between them. 1. the usage of ++ I (take a ++ I, i2 as an example) first adds the I value to 1 (that is, ii + 1) and then assigns

In many programming languages, ++ I and I ++ add + 1 operations to variables, but there are successive problems. next I will introduce some differences between them.

1. usage of ++ I (using a = ++ I, I = 2 as an example)

Add the I value to 1 (that is, I = I + 1) and then assign it to variable a (that is, a = I ),

The final a value is 3, and the I value is 3.

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

2. I ++ usage (take a = I ++, I = 2 as an example)

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

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 independently, they are equivalent to I = I + 1.

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

Performance Optimization

The instance code is as follows:

  1. Method 1:
  2.  
  3. $ Begin = time ();
  4. $ I = 0;
  5. While (++ $ I <10000)
  6. {
  7. $ J = 0;
  8. While (++ $ j <10000)
  9. ;
  10. ;
  11. }
  12. $ End = time ();
  13.  
  14. Time: 16 s
  15.  
  16. Method 2:
  17.  
  18. $ Begin = time ();
  19. $ I = 0;
  20. While ($ I <10000)
  21. {
  22. $ J = 0;
  23. While ($ j <10000)
  24. + + $ J;
  25. ++ $ I;
  26. }
  27. $ End = time ();
  28.  
  29. Time: 13 s
  30.  
  31. Method 3:
  32.  
  33. $ Begin = time ();
  34. $ I = 0;
  35. While ($ I <10000)
  36. {
  37. $ J = 0;
  38. While ($ j <10000)
  39. $ J ++;
  40. $ I ++;
  41. }
  42. $ End = time ();
  43.  
  44. Time: 15 s
  45.  
  46. Method 4:
  47.  
  48. $ Begin = time ();
  49. $ I = 0;
  50. While ($ I ++ <10000)
  51. {
  52. $ J = 0;
  53. While ($ j ++ <10000)
  54. ;
  55. ;
  56. }
  57. $ End = time ();

Time: 13 s

Compare the first and second methods, because in PHP, the OPCODE is finally executed, and each row of opline has two operands. for the operands, there are generally three types of access methods, temporary variables, variables, and compile-time variables. among these three variables, the fastest access is the third, compiler variables, during OpCode execution, a variable's plus-level reference is stored in a hash

Structure, used to speed up access.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.