When it comes to calculating the parity of numbers, it is common to use "and" and "modulo", so which of these two operations are implemented in PHP language, which is faster? Before the comparison, I was inclined to the former. But it turned out to be a surprise to me. The code is as follows:
"and" Operations:
<?phpset_time_limit (0); $i = 1; $t 1 = Microtime (true); while ($i < 1000000001) {$i + + & 1;} $t 2 = Microtime (true), echo $t 2-$t 1;
Results: 650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/77/11/wKioL1Zid_Hzm_AhAAAEP29JfQc592.png "title=" 3. PNG "alt=" Wkiol1zid_hzm_ahaaaep29jfqc592.png "/>
"Modulo" operations:
<?phpset_time_limit (0); $i = 1; $t 1 = Microtime (true); while ($i < 1000000001) {$i + + + 2;} $t 2 = Microtime (true), echo $t 2-$t 1;
Results: 650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/77/12/wKiom1Zid_aS9b4_AAAEoMEHkRM261.png "title=" 4. PNG "alt=" Wkiom1zid_as9b4_aaaeomehkrm261.png "/>
This article from the "10921582" blog, reproduced please contact the author!
Comparison of 1 billion times comparison between--php "and" and "modulo" operation efficiency