start(); } /** * 开始计时 */ public function start() { $this->start = microsecond(); $this->end = null; } /** * 停止计时 */ public function end() { $this->end = microsecond(); } /** * 返回start和end之间消耗的时间 * @param boolean $showUnit 是否附加单位(ss)字符串 * @return int start和end见消逝的微秒数.
*/ public function getRenderTime($showUnit = true) { $test = is_float($this->start) && is_float($this->end) && $this->start <= $this->end; if ($test) { $duration = round(($this->end - $this->start), 5); # 精确到小数点后5位 if ($showUnit) { # 附加显示单位字符串 return number_format($duration) . '微秒. '; } else { return $duration; } } else { return 'Can\'t calc the render time!'; } } /** * 测试 * @param callable $func 待测试代码 * @param int $times 次数 default(1000), Ps. php性能还是欠奉,循环1千次也就得了, 1万次经查挂.o(╯□╰)o */ public static function Test($func, $times = 1000) { if (is_callable($func)) { $n = intval($times); # 防御错误的输入 $t = new RenderTime(); for ($i = 0; $i < $n; $i++) { $func(); } $t->end(); echoLine('memory: ' . number_format(memory_get_peak_usage() / 1024) . "kb, looped: $i, timesp: " . $t->getRenderTime()); } else { echoLine("$func is not callable!"); } } } /** * 测试 * @param callable $func 待测试代码 * @param int $times 次数 default(1000), Ps. php性能还是欠奉,循环1千次也就得了, 1万次经常挂.o(╯□╰)o */ function test($func, $times = 1000) { RenderTime::Test($func, $times); }