I need to determine how long a PHP function has been running so far.
我需要确定到目前为止PHP函数运行了多长时间。
What are some options to find out how long some PHP code takes to run?
有哪些选项可以找出一些PHP代码运行多长时间?
I'm using zend framework.
我正在使用zend框架。
4 个解决方案
#1
27
Call microtime(true)
function to fetch current time with milisecond resolution.
调用microtime(true)函数以获取具有毫秒分辨率的当前时间。
<?php
$startTime = microtime(true);
/*stuff is going on*/
echo "Elapsed time is: ". (microtime(true) - $startTime) ." seconds";
#2
2
microtime — Return current Unix timestamp with microseconds
microtime - 返回当前的Unix时间戳,以微秒为单位
<?php
$time = microtime(TRUE);
//...code here...
print (microtime(TRUE)-$time). ' seconds';
#3
0
You could simply use PHP's standard timeout, and implement a shutdown function.
您可以简单地使用PHP的标准超时,并实现关闭功能。
function say_goodbye() {
if (connection_status() == CONNECTION_TIMEOUT) {
... do your special processing to save the process state here
... then formulate a response for the browser
}
} // function say_goodbye()
register_shutdown_function("say_goodbye");
Note that you can set the shutdown function to accept parameters
请注意,您可以将关闭功能设置为接受参数
EDIT
编辑
function say_goodbye($controller1,$controller2) {
if (connection_status() == CONNECTION_TIMEOUT) {
... do your special processing to save the process state here
... then formulate a response for the browser
}
} // function say_goodbye()
$ctl1 = new DBController();
$ctl2 = new OPController();
register_shutdown_function("say_goodbye",$ctl1,$ctl2);
#4
0
One of quick and accurate snippet I can recommend.
我可以推荐一个快速准确的代码片段。
//Block of my complex code
//...
echo 'Exact time taken to run: ' . (microtime(true) - $_SERVER['REQUEST_TIME_FLOAT']) . ' seconds';
#1
27
Call microtime(true)
function to fetch current time with milisecond resolution.
调用microtime(true)函数以获取具有毫秒分辨率的当前时间。
<?php
$startTime = microtime(true);
/*stuff is going on*/
echo "Elapsed time is: ". (microtime(true) - $startTime) ." seconds";
#2
2
microtime — Return current Unix timestamp with microseconds
microtime - 返回当前的Unix时间戳,以微秒为单位
<?php
$time = microtime(TRUE);
//...code here...
print (microtime(TRUE)-$time). ' seconds';
#3
0
You could simply use PHP's standard timeout, and implement a shutdown function.
您可以简单地使用PHP的标准超时,并实现关闭功能。
function say_goodbye() {
if (connection_status() == CONNECTION_TIMEOUT) {
... do your special processing to save the process state here
... then formulate a response for the browser
}
} // function say_goodbye()
register_shutdown_function("say_goodbye");
Note that you can set the shutdown function to accept parameters
请注意,您可以将关闭功能设置为接受参数
EDIT
编辑
function say_goodbye($controller1,$controller2) {
if (connection_status() == CONNECTION_TIMEOUT) {
... do your special processing to save the process state here
... then formulate a response for the browser
}
} // function say_goodbye()
$ctl1 = new DBController();
$ctl2 = new OPController();
register_shutdown_function("say_goodbye",$ctl1,$ctl2);
#4
0
One of quick and accurate snippet I can recommend.
我可以推荐一个快速准确的代码片段。
//Block of my complex code
//...
echo 'Exact time taken to run: ' . (microtime(true) - $_SERVER['REQUEST_TIME_FLOAT']) . ' seconds';