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

yipeiwu_com6年前PHP代码库

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

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

<?php
$long_str = "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";
?>

运行结果如下:

That took 7.1525573730469E-6 seconds.

php 计算函数执行时间的方法及获得微妙的方法

// 获得微妙方法
 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<=1000;$i++){
echo $i.'<br>';
}
$end_time = microtime(true);
echo '循环执行时间为:'.($end_time-$start_time).' s';
?>

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

相关文章

IIS安装Apache伪静态插件的具体操作图文

IIS安装Apache伪静态插件的具体操作图文

Apache和IIS分别有自己的伪静态操作方法,那在Servers2003_IIS需要给PHP程序使用伪静态呢?安装rewrite插件包。一、下载rewrite插件包,一般里面必须有ht...

《Head First 设计模式》代码之PHP版(面向对象学习)第1/2页

书中的例子都比较浅显易懂,不过由于是外国佬写的,所以例子的习惯不是很附合中国特色,可能偶尔看起来有些别扭,还有语言习惯也不是中国风。当然��看过这本书之后,你...

php查找字符串中第一个非0的位置截取

php查找字符串中第一个非0的位置截取

话不多说,请看代码: $str = '00000000000000000000000000000000000000001234506'; $preg = '/[0]*/'; $res...

又十个超级有用的PHP代码片段

好东西要大家一起分享,上次分享了十个,这次再来十个超级有用的PHP代码片段。 1. 发送短信 调用 TextMagic API。 // Include the TextMagic P...

实测在class的function中include的文件中非php的global全局环境

测试代码1.php 复制代码 代码如下: <?php $g1 = 'g1'; class c{ function fun() { include('2.php'); echo "\...