Home >Backend Development >PHP Tutorial >Use PHP's built-in Math function with caution_PHP Tutorial
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~~