数学 (math) 函数能处理 integer 和 float 范围内的值。
这篇文章主要介绍了php内置的math函数效率测试,以实例形式测试了相关的php内置数学运算函数的执行时间,分析其运行效率,需要的朋友可以参考下
代码如下:
$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
再对比下用math函数的代码和结果,代码如下:
$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
看到木有,效率提升100%!!以前还一直都认为是php内置的math快,真是不测不知道,像取绝对值abs,最大值max,最小值min 等效率都不如原生的 if判断来得快.
以上就是php math函数详解的详细内容。