PHP built-in Math Function efficiency test

Source: Internet
Author: User
This article mainly introduces the built-in Math Function efficiency test in PHP, and tests the execution time of related PHP built-in mathematical functions in the form of examples to analyze the running efficiency. For more information, see

This article mainly introduces the built-in Math Function efficiency test in PHP, and tests the execution time of related PHP built-in mathematical functions in the form of examples to analyze the running efficiency. For more information, see

This article analyzes the efficiency of the built-in Math Function in PHP. Share it with you for your reference. The specific analysis is as follows:

As shown in the question, for friends who have not done large-scale operations, they may not know that the Math Function operation in PHP is so slow. You still have to worry about writing a few more words, the Code is as follows:

The Code is as follows:

$ 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 result of using the Math function. The Code is as follows:

The Code is as follows:

$ 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


As you can see, the efficiency is improved by 100% !! Previously, I always thought it was PHP's built-in Math speed, but I don't know if it was really unexpected. For example, taking the absolute value abs, the maximum value max, and the minimum value min is not as efficient as determining the native if condition.

In general, php operations are really slow and really not suitable for large-scale algorithm operations. I hope this article will help you with PHP programming.

,

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.