Use the built-in Math Function of PHP with caution

Source: Internet
Author: User

The article details some of the usage procedures of PHP's built-in Math functions. I hope some methods will be helpful to you.

As shown in the following figure... I do not know how to perform large-scale operations... The Math Function operation in PHP is so slow... You are still in trouble. You need to write a few more words in hand to calculate the number of birds ~~~

Xiao 'er! On the code ....

 

The Code is as follows: Copy code
$ Start = microtime (TRUE );
For ($ I = 0; I I <200000; $ I ++ ){
$ S = 0; for ($ j = 0; $ j <3; $ j ++ ){
$ S + = ($ j + $ I + 1) * ($ j + $ I + 1 );}
}
Echo microtime (TRUE)-$ start; # output: 0.33167719841003

 

Then compare the code and results of using the Math Function

The Code is as follows: Copy code
$ Start = microtime (TRUE );
For ($ I = 0; I I <200000; $ I ++ ){
$ S = 0; for ($ j = 0; $ j <3; $ j ++ ){
$ S + = pow ($ j + $ I + 1, 2 );}
}
Echo microtime (TRUE)-$ start; # output: 0.87528896331787

 

The efficiency is improved by 100%... I used to think it was PHP's built-in Math that was fast, but I don't know if I did .. Such as taking absolute abs, maximum max, and minimum min values is less efficient than native if to judge faster ~~

In general, php operations are really slow ~~ It is really not suitable for large-scale algorithm operations ~~


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.