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

yipeiwu_com6年前PHP代码库

核心代码

<?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).'秒';

?>

相关文章

JoshChen_php新手进阶高手不可或缺的规范介绍

PHP规范1. 为什么要编码规范•编码规范(code conventions)对于程序员而言尤为重要,有以下几个原因:1.在一个软件的生命周期中,80%的花费用于维护。 2....

PHP 数组遍历方法大全(foreach,list,each)

在PHP中数组分为两类: 数字索引数组和关联数组。 其中数字索引数组和C语言中的数组一样,下标是为0,1,2… 而关联数组下标可能是任意类型,与其它语言中的hash,map等结构相似。...

php中json_encode处理gbk与gb2312中文乱码问题的解决方法

本文讲述了php中json_encode处理gbk与gb2312中文乱码问题的解决方法,具体方法如下: 1.json_encode()中文在gbk/gb2312中对中文返回为null...

php数组键名技巧小结

本文较为详细的总结了php数组键名的技巧。分享给大家供大家参考。具体分析如下: 1、$arr[true] 等价于 $arr[1];$arr[false] 等价于 $arr[0]。 2、使...

php5.2以下版本无json_decode函数的解决方法

今天写代码的时候,需要用到json_decode函数,发现php5.2以前的版本没有集成这个函数,不过我们可以通过自定义函数实现。 复制代码 代码如下:function json_dec...