计算 PHP 脚本执行时间

使用的函数是 microtime(),这个函数会以 "msec sec" ( 当前 Unix 时间戳和微秒数 ) 的格式返回一个字符串。

代码如下:

<?php

  function
getCurrentTime (){
  
list ($msec,$sec) = explode(" ", microtime());   return (float)$msec + (float)$sec;   }   //使用方法   $begin = getCurrentTime();
    
//...这里是需要计算执行时间的程序,例如:
    sleep(3);
  $end = getCurrentTime();
  
$spend = $end-$begin;   echo "脚本执行时间为:".$spend."\n";

页面显示:

脚本执行时间为:2.9997780323029

 

以上内容的原文地址:http://blog.csdn.net/wzy_1988/article/details/8502685

 

如果显示的时间方式为科学计数法,例如 脚本执行时间为:5.9843063354492E-5,可以利用以下的代码将科学计数法换原成原始数字:

echo "脚本执行时间为:".sprintf("%0.9f", $spend)."\n";

页面显示:脚本执行时间为:0.000059843

注:sprintf() 函数把格式化的字符串写入一个变量中,%f 表示转换的格式是浮点数,0.9表示小数点后保留几位有效数字。

 

posted @ 2014-10-22 14:39  nemo20  阅读(302)  评论(0)    收藏  举报
访客数:AmazingCounters.com
2016/05/17 起统计