Home  >  Article  >  Backend Development  >  Use PHP’s built-in Math function with caution_PHP Tutorial

Use PHP’s built-in Math function with caution_PHP Tutorial

WBOY
WBOYOriginal
2016-07-20 10:59:13798browse

This article introduces in detail some issues in the use process of PHP's built-in Math function. I hope these methods will be helpful to you. ​

As shown in the title. . . I haven't done any large-scale calculations, so I don't know yet. . . Damn, PHP's Math function operation turns out to be so slow. . . It’s better for everyone to go to trouble and write a few more sentences by hand~~~

Waiter! Up code. . . .

 代码如下 复制代码
$start = microtime(TRUE);  
for ($i=0; $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 

Compare the code and results using the Math function

 代码如下 复制代码
$start = microtime(TRUE);  
for ($i=0; $i < 200000; $i++)   {  
    $s = 0;       for ($j=0; $j < 3; $j++)       {   
       $s += pow($j+$i+1, 2);       }  
}  
echo microtime(TRUE) – $start;   # output: 0.87528896331787 

Seeing that there is no problem, the efficiency is increased by 100%. . . In the past, I always thought that it was PHP's built-in Math speed, but I didn't know it was unexpected. . For example, taking absolute value abs, maximum value max, minimum value min, etc. are not as efficient as the original if judgment~~

In general, PHP calculations are indeed very slow~~ It is really not suitable for large-scale algorithm calculations~~


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445619.htmlTechArticleThis article introduces in detail some problems in using the built-in Math function in PHP. I hope these methods will be helpful to you. . As the title indicates. . . I haven't done any large-scale calculations, so I don't know yet. . . ...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn