If/else implementation mechanism in PHP

Source: Internet
Author: User
In PHP, the ifelse execution mechanism is as follows: In php, the test is that if branch is 2.9 seconds slow, else branch is 2.2 seconds fast, and if conditions are in 11 and 1! 1. what mechanism is this? PHPcode $ stimemicrotime (true); get the execution start time of the program $ j0; for ($ i0; $ I & lt; 5000000; $ I ++) {if/else execution mechanism in PHP
The following code shows that in php, if branch is slow for 2.9 seconds, else branch is fast for 2.2 seconds, and if conditions are set to 1 = 1 and 1! = 1 transformation. what mechanism is this?
PHP code
  $ Stime = microtime (true); // Obtain the execution start time of the program $ j = 0; for ($ I = 0; $ I <5000000; $ I ++) {if (1! = 1) {$ j ++;} else $ j ++;} $ etime = microtime (true ); // Obtain the execution end time of the program $ total = $ etime-$ stime; // calculate the difference value $ str_total = var_export ($ total, TRUE); if (substr_count ($ str_total, "E") {$ float_total = floatval (substr ($ str_total, 5); $ total = $ float_total/100000;} echo "$ total ". 'second ';


------ Solution --------------------
Tested, not what you said.
PHP code
/* If 0.55976009368896 seconds 0.55944108963013 seconds 0.56911516189575 seconds 0.5602080821991 seconds 0.56702303886414 seconds 0.56633019447327 seconds else0.5650839805603 seconds 0.56741714477539 seconds 0.56370115280151 seconds 0.56349802017212 seconds */
------ Solution --------------------
If else execution efficiency is different only in the if code block and the else code block, right?
The two pieces of code here are the same. the execution time should be roughly the same.
------ Solution --------------------
The result is produced by curly brackets. php code is not the lines we seem to have. php will eventually run in C. Therefore, every line of every symbol, C should be converted and interpreted.
------ Solution --------------------
Such a question should be answered by the blame...

Discussion

If php executes C, can all c optimization solutions be applied? However, conversion also takes time. how can we comprehensively determine the performance and find the breakthrough point for performance improvement ~ Please advise

------ Solution --------------------
Because if is to be determined, else does not need to be judged. you can try else if to see it, it is slower, and = and! = It's no difference between 0 and 1.

If the design of an if... else statement becomes a performance problem, the language can be abandoned.
------ Solution --------------------
Discussion

If php executes C, can all c optimization solutions be applied? However, conversion also takes time. how can we comprehensively determine the performance and find the breakthrough point for performance improvement ~ Please advise

------ Solution --------------------
The execution code in the if and else blocks is equal and the efficiency is equal.
------ Solution --------------------
Discussion

The execution code in the if and else blocks is equal and the efficiency is equal.

------ Solution --------------------
Discussion

Reference:

Because if is to be determined, else does not need to be judged. you can try else if to see it, it is slower, and = and! = It's no difference between 0 and 1.

If the design of an if... else statement becomes a performance problem, the language can be abandoned.


At first, I had a hard time figuring out these small details, but I thought it might be a little weak in performance optimization. if I want to do performance optimization, where should I start?
......

------ Solution --------------------
Continue, learn more

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.