Comparison of 1 billion times comparison between--php "and" and "modulo" operation efficiency

Source: Internet
Author: User
Tags php language

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

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.