Saturday, April 24, 2010

Finding slow part of your php code

Sometimes we see that our php script is running very slow, we, just think that may be reason is our loop and sql query etc . But we do not know that where is actual problem.
In which function or method execution is very slow.
Here is a little code which check the speed of your php script,

Define this function on top of your script or in any other file and include that in your page,

<?php function speedcheck() {

static $str = NULL;

if( is_null($str) ) {

$str = microtime();

} else {

$speedcheck = microtime() - $str;

$str = microtime();

return $speedcheck;

}

}



function microtime() {

list($uses, $second) = explode(" ", microtime());

return ((float)$uses + (float)$second);

} ?>




After that call this function after execution of your function and methods, this will return the execution time. and then you check performance of your code.

<?php

speedcheck(); /* The first call will initialize the function */



/* Some code */



echo speedcheck() .'<br />';



/* Some more code */



echo speedcheck() .'<br />';



/* Yet more code */



echo speedcheck() .'<br />'; ?>

No comments:

Post a Comment