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!