We need to take care of the execution time (minimize it) to ensure that the script is not Timed Out in the server.
Doing this is quite simple.
Just a php function microtime( ) is enough to do this.
Here is the example :
$start_time = microtime(true);// Do some work or your whole script$end_time = microtime(true);$execution_time = $end_time - $start_time ;
Now, the function's details
microtime
(PHP 4, PHP 5)
microtime — Return current Unix timestamp with microseconds
Description
mixed microtime ([ bool $get_as_float ] )
microtime() returns the current Unix timestamp with microseconds. This function is only available on operating systems that support the gettimeofday() system call.
Parameters
get_as_float
When called without the optional argument, this function returns the string "msec sec" where sec is the current time measured in the number of seconds since the Unix Epoch (0:00:00 January 1, 1970 GMT), and msec is the microseconds part. Both portions of the string are returned in units of seconds.
If the optional get_as_float is set to TRUE then a float (in seconds) is returned.
And now, here is an Object Oriented approach of doing thisIf the optional get_as_float is set to TRUE then a float (in seconds) is returned.
example :
$timer = new Timer(); $timer->start();// Add the codes whose time you need to count$timer->stop(); echo "elapsed time ".$timer->getTime() ; // Use method getTime() to get the elapsed time in seconds $timer>printTime(); // Use printTime() to echo the elapsed time//like, Elapsed Time : 0.0893499851227 seconds//And, here is the class :class Timer { var $clock = 0; var $elapsed = 0 ; function start () { $this->clock = microtime(true); $this->elapsed = 0 ; return true ; } function stop () { if($this->clock == 0) return false ; $this->elapsed = microtime(true) - $this->clock ; return true ; } function getTime(){ return $this->elapsed ; } function printTime(){ if($this->elapsed != 0){ $txt = "Elapsed Time : " . $this->elapsed . " seconds"; }else if($this->clock == 0){ $txt = "Counter Not started yet"; }else{ $txt = "Counter started but Not stopped"; } echo " ".$txt." "; return $txt ; } }
0 comments :: how to calculate script's execution time in php
Post a Comment