当前位置: 移动技术网 > IT编程>开发语言>PHP > PHP 一个页面执行时间类代码

PHP 一个页面执行时间类代码

2019年05月01日  | 移动技术网IT编程  | 我要评论

核心代码

<?php 
class timer//页面执行时间类 
{ 
var starttime;//页面开始执行时间 
var stoptime;//页面结束执行时间 
var spendtime;//页面执行花费时间 
function getmicrotime()//获取返回当前微秒数的浮点数 
{ 
list(usec,sec)=explode(" ",microtime()); 
return ((float)usec + (float)sec); 
} 
function start()//页面开始执行函数,返回开始页面执行的时间 
{ 
this->starttime=this->getmicrotime(); 
} 
function display()//显示页面执行的时间 
{ 
this->stoptime=this->getmicrotime(); 
this->spendtime=this->stoptime-this->starttime; 
return round(this->spendtime,10); 
} 
} 
/*调用方法 
timer=new timer(); 
timer->start(); 
/*在此处放入你要执行的脚本或代码 
for(i=0;i<100000;i++) 
{ 
echo i; 
echo "<br>"; 
} 
*/ 
//echo "<p>执行该代码花费时间".timer->display()."秒"; 
?> 

php检测每一段代码执行时间

<?php
// 实例1

/**
 * @start time
 */
function prostarttime() {
  global $starttime;
  $mtime1 = explode(" ", microtime());
  $starttime = $mtime1[1] + $mtime1[0];
}

/**
 * @end time
 */
function proendtime() {
  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

$t1 = microtime(true);
sleep(3);
$t2 = microtime(true);
echo '程序耗时'.round($t2-$t1,3).'秒';

?>

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网