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

(编辑:jimmy 日期: 2025/6/28 浏览:2)

本文实例讲述了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程序设计有所帮助。

一句话新闻

微软与英特尔等合作伙伴联合定义“AI PC”:键盘需配有Copilot物理按键
几个月来,英特尔、微软、AMD和其它厂商都在共同推动“AI PC”的想法,朝着更多的AI功能迈进。在近日,英特尔在台北举行的开发者活动中,也宣布了关于AI PC加速计划、新的PC开发者计划和独立硬件供应商计划。
在此次发布会上,英特尔还发布了全新的全新的酷睿Ultra Meteor Lake NUC开发套件,以及联合微软等合作伙伴联合定义“AI PC”的定义标准。