Home > Backend Development > PHP Tutorial > PHP内置的Math函数效率测试_PHP

PHP内置的Math函数效率测试_PHP

WBOY
Release: 2016-05-31 18:16:01
Original
927 people have browsed it

本文实例分析了PHP内置的Math函数效率问题。分享给大家供大家参考。具体分析如下:

如题所示,对于没有做过大规模运算的朋友来说,可能还不知道,PHP的Math函数运算原来是如此之慢的,大家还是麻烦点,手写多几句,代码如下:

代码如下:

$start = microtime(TRUE);   
for ($i=0; $i     $s = 0;
    for ($j=0; $j        $s += ($j+$i+1) * ($j+$i+1);
    }   
}   
echo microtime(TRUE) – $start;  // output: 0.33167719841003


再对比下用Math函数的代码和结果,代码如下:

代码如下:

$start = microtime(TRUE);   
for ($i=0; $i     $s = 0;
    for ($j=0; $j        $s += pow($j+$i+1, 2);
    }
}
echo microtime(TRUE) – $start;   // output: 0.87528896331787


看到木有,效率提升100%!!以前还一直都认为是PHP内置的Math快,真是不测不知道,像取绝对值abs,最大值max,最小值min 等效率都不如原生的 if判断来得快.

总的来说,php运算的确是很慢,真心不适合做大规模的算法运算.希望本文所述对大家的PHP程序设计有所帮助。

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template