Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine how long PHP executed so far

Tags:

function

php

I need to determine how long a PHP function has been running so far.

What are some options to find out how long some PHP code takes to run?

I'm using zend framework.

like image 908
jmint Avatar asked Jan 25 '11 17:01

jmint


People also ask

How does PHP calculate time of execution?

Clock time can get using microtime() function. First use it before starts the script and then at the end of the script. Then using formula (End_time – Start_time). The mirotime() function returns time in seconds.

What is PHP execution time?

Remember, the max execution time of a PHP script is 30 seconds.

How do I know if PHP script is running on server?

Replace #user# with your script user. Show activity on this post. The above code lists all php processes running in full, then checks to see if "my_script. php" is in the list of running processes, if not it runs the process and does not wait for the process to terminate to carry on doing what it was doing.

How can you increase the maximum execution time of Ascript in PHP?

To increase the script execution time, you can use the following snippet at the top of your script. ini_set ( 'max_execution_time' , '300' ); In the above example, it would set the max_execution_time directive to 300 seconds.


3 Answers

Call microtime(true) function to fetch current time with milisecond resolution.

<?php
$startTime = microtime(true);

/*stuff is going on*/

echo "Elapsed time is: ". (microtime(true) - $startTime) ." seconds";
like image 102
Mchl Avatar answered Oct 27 '22 18:10

Mchl


microtime — Return current Unix timestamp with microseconds

<?php
$time = microtime(TRUE);

//...code here...

print (microtime(TRUE)-$time). ' seconds';
like image 32
Yada Avatar answered Oct 27 '22 16:10

Yada


You could simply use PHP's standard timeout, and implement a shutdown function.

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);
like image 38
Mark Baker Avatar answered Oct 27 '22 18:10

Mark Baker