Php:switch-case and If-else Two efficiency who is taller

Source: Internet
Author: User
In the morning to write a PHP program, because to deal with a large amount of data, so have to consider the issue of running time. Then in the calculation of the weight of each item encountered a problem, in the consideration should be used switch-case or If-else, in the online look, we all said switch-case efficiency than if-else High, I wrote a few programs tested a bit, is this, In particular, in a large number of data and multiple conditions switch-case performance is much higher than if-else, in fact, this is understandable, because the switch-case comparison of the sentence is very concentrated, so that the cache will not continue to compare and determine the branch and the refresh occurs. But the comparative judgment of the if-else part is more scattered, each time the comparison to jump to a very far place, so every time (almost every time) need to refresh the cache, naturally caused by slow.

Here's a little something from the compilation principle of "stealing":

Switch (val) {case   1: ...; Break Case   2: ...; Break Case   4: ...; Break Case   6: ...; Break Case   7: ...; Break Default: ...; The corresponding assembly is addr_tab:               dd   def_addr               DD   addr1               DD   addr2               DD   def_addr               DD   ADDR4               DD   def_addr               DD   addr6               DD   ADDR7               cmp     val,7               ja       def_addr               jmp     DWORD   ptr   [eax*4+addr_tab]               addr1: ...                 .               ADDR2: ...                 .               ADDR4: ...                 .               ADDR6: ...                 .               ADDR7: ...                 .                 DEF_ADDR: ...                   .

Switch compares if has the speed advantage, one is takes the transfer address list the method, also is the switch generally in the loose situation also does not take "the comparison-transfer" The method, but uses the DEC (sub)-JZ instruction pair, the latter is not only the instruction length to shorten, the speed also has the superiority.

about the test of the program is not posted up, should be interested in the test itself, OK, decided to use switch-case!

Related Article

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.