在写这个类之前,我来介绍一下php的一个内置函数microtime(),详情请查看php手则。而php5新增 microtime(true) 返回当前时间的浮点数,单位是微秒。
对于这个类算法其实很简单 就是
现在的时间-开始的时间=花费的时间
so,上代码 嘎嘎 !
<?php class timer { private $StartTime = 0;//程序运行开始时间 private $StopTime = 0;//程序运行结束时间 private $TimeSpent = 0;//程序运行花费时间 function start(){//程序运行开始 $this->StartTime = microtime(true); } function stop(){//程序运行结束 $this->StopTime = microtime(true); } function spent(){//程序运行花费的时间 $this->TimeSpent=$this->StopTime-$this->StartTime; return number_format($this->TimeSpent*1000, 4).'毫秒';//返回获取到的程序运行时间差 } } /*$timer = new timer(); $timer->start(); //所要执行的程序 $timer->stop(); echo "程序运行时间为:".$timer->spent();*/ ?>