网络编程 
首页 > 网络编程 > 浏览文章

php计算函数执行时间的方法

(编辑:jimmy 日期: 2025/9/21 浏览:3 次 )

本文实例讲述了php计算函数执行时间的方法。分享给大家供大家参考。具体如下:

我们可以通过在程序的前后分别记录开始和结束时间,两个时间差就是程序的执行时间。

<"this is a test to see how much time md5 function takes to execute over this string";
// start timing from here
$start = microtime(true);
// function to test
$md5 = md5($long_str);
$elapsed = microtime(true) - $start;
echo "That took $elapsed seconds.\n";
"htmlcode">
// 获得微妙方法
 function getMillisecond()
 {
   list($s1, $s2) = explode(' ', microtime());
   return (float)sprintf('%.0f', (floatval($s1) + floatval($s2)) * 1000);
 }

原理:分别记录函数开始时间和结束时间,然后时间差就是函数执行的时间

<?php
 $start_time = microtime(true);
for($i=1;$i&lt;=1000;$i++){
echo $i.'&lt;br&gt;';
}
$end_time = microtime(true);
echo '循环执行时间为:'.($end_time-$start_time).' s';
?>

希望本文所述对大家的php程序设计有所帮助。

上一篇:php使用mysqli向数据库添加数据的方法
下一篇:php的4种常见运行方式
Windows上运行安卓你用过了吗
在去年的5月23日,借助Intel Bridge Technology以及Intel Celadon两项技术的驱动,Intel为PC用户带来了Android On Windows(AOW)平台,并携手国内软件公司腾讯共同推出了腾讯应用宝电脑版,将Windows与安卓两大生态进行了融合,PC的使用体验随即被带入到了一个全新的阶段。