本文使用「署名 4.0 国际 (CC BY 4.0)」许可协议,欢迎转载、或重新修改使用,但需要注明来源。 [署名 4.0 国际 (CC BY 4.0)](https://creativecommons.org/licenses/by/4.0/deed.zh) 本文作者: 苏洋 创建时间: 2012年02月07日 统计字数: 1022字 阅读时间: 3分钟阅读 本文链接: https://soulteary.com/2012/02/07/php-show-run-time.html ----- # PHP 程序运行时间 常常看到页面执行完毕,底下有一行,程序执行时间XXX吧.有的时候,用来比较某些函数运行效率,这也不失为一个好办法。 放2种方法,一种是简单的语句模块,一种是简单的类。 这种很简单,适合在几个简单的页面内使用.如果结果太小,那么就调整循环的数量 ```php ``` 这个类的好处就是可以安全的调用,而不必担心变量被修改 这个类的出处:[这里](http://soulteary.com/redirect?url=http%3A%2F%2Fwww.CodeBit.cn&key=c6306f6c6c13aaef84cb9d0b74f0a013)> ```php StartTime = $this->get_microtime(); } function stop() { $this->StopTime = $this->get_microtime(); } function spent() { return round(($this->StopTime - $this->StartTime) * 1000, 1); } } /* //例子 $timer = new timer; $timer->start(); //你的代码开始 $a = 0; for($i=0; $i<1000000; $i++) { $a += $i; } //你的代码结束 $timer->stop(); echo "页面执行时间: ".$timer->spent()." 毫秒"; */ ?> ```