核心代码:
- <?php
 - classTimer//页面执行时间类
 - {
 - varstarttime;//页面开始执行时间
 - varstoptime;//页面结束执行时间
 - varspendtime;//页面执行花费时间
 - functiongetmicrotime()//获取返回当前微秒数的浮点数
 - {
 - list(usec,sec)=<a href="/tags.php/explode/" target="_blank">explode</a>(" ",microtime());
 - return((float)usec + (float)sec);
 - }
 - functionstart()//页面开始执行函数,返回开始页面执行的时间
 - {
 - this->starttime=this->getmicrotime();
 - }
 - functiondisplay()//显示页面执行的时间
 - {
 - this->stoptime=this->getmicrotime();
 - this->spendtime=this->stoptime-this->starttime;
 - returnround(this->spendtime,10);
 - }
 - }
 - /*调用方法
 - timer=new Timer();
 - timer->start();
 - /*在此处放入你要执行的脚本或代码
 - for(i=0;i<100000;i++)
 - {
 - echo i;
 - echo "<br>";
 - }
 - */
 - //echo "
 - 执行该代码花费时间".timer->display()."秒";
 - ?>
 
PHP检测每一段代码执行时间:
- <?php
 - // 实例1
 - /**
 - * @start time
 - */
 - functionproStartTime() {
 - global$startTime;
 - $mtime1=explode(" ", microtime());
 - $startTime=$mtime1[1] +$mtime1[0];
 - }
 - /**
 - * @End time
 - */
 - functionproEndTime() {
 - global$startTime,$set;
 - $mtime2=explode(" ", microtime());
 - $endtime=$mtime2[1] +$mtime2[0];
 - $totaltime= ($endtime-$startTime);
 - $totaltime= number_format($totaltime, 7);
 - echo"<br>process time: ".$totaltime;
 - }
 - // 程序调用开始记时
 - proStartTime();
 - sleep(1); // sleep() 延时代码执行若干秒
 - proEndTime();// 程序在每一段所消耗的执行时间
 - sleep(2);
 - proEndTime();
 - sleep(3);
 - proEndTime();
 - /************************************************* 华丽的分割线 **************************************************/
 - // 实例2
 - //Vevb.com
 - $t1= microtime(true);
 - sleep(3);
 - $t2= microtime(true);
 - echo'程序耗时'.round($t2-$t1,3).'秒'
 - ?>
 
新闻热点
疑难解答