gpt4 book ai didi

PHP代码执行时间

转载 作者:可可西里 更新时间:2023-10-31 22:17:47 25 4
gpt4 key购买 nike

如何找到我的代码执行时间?

我正在使用以下代码段。

我不满意吗?

list ($msec, $sec) = explode(' ', microtime());
$microtime = (float)$msec + (float)$sec;

最佳答案

我为此创建了这个简单的类:

class timer
{
private $start_time = NULL;
private $end_time = NULL;

private function getmicrotime()
{
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}

function start()
{
$this->start_time = $this->getmicrotime();
}

function stop()
{
$this->end_time = $this->getmicrotime();
}

function result()
{
if (is_null($this->start_time))
{
exit('Timer: start method not called !');
return false;
}
else if (is_null($this->end_time))
{
exit('Timer: stop method not called !');
return false;
}

return round(($this->end_time - $this->start_time), 4);
}

# an alias of result function
function time()
{
$this->result();
}

}

要计时脚本,您可以像这样使用它:

$timer = new timer();

$timer->start();
// your code now
$timer->stop();

// show result now
echo $timer->result();

关于PHP代码执行时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2942385/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com