统计PHP代码的执行时间

执行PHP运行时间统计的代码大家看看吧,话不多看代码
class TimeMark { private $last; private $n; public function start() { $this->last = microtime(true); $this->n = 1; } public function mark($name) { $now = microtime(true); $name = str_replace(' ', '-', $name); header("X-mark-{$this->n}-{$name}:" . ($now-$this->last)); $this->last = $now; ++ $this->n; } }

 

示例:

$mark = new TimeMark(); $mark->start(); $s = ''; for($i=0;$i<10000;++$i) { $s .= '****'; } $mark->mark('concat with for loop'); $s = str_repeat('****', 10000); $mark->mark('str_repeat'); 

 

打印结果


X-mark-1-concat-with-for-loop 0.0038130283355713 
X-mark-2-str_repeat 0.00021910667419434

 

你可能感兴趣的:(PHP)