Dec

22

Sometimes when you do performence tuning, you would like to see how long a function takes to execute. To determine how much time a single function takes to execute, you can either use a full benchmarking package like PEAR Benchmark, or instead, you can get the information you need from the PHP build-in function - microtime( ) .

Let’s see how microtime( ) works, below is a demo to calculate the time takes to execute PHP’s basic md5( ) function and hash( )function in PHP 5.1.2,

<!--p<--> $long_str = 'Crosby, Stills, Nash, and Young';

$start = microtime(true);
$hashA = md5($long_str);
$elapsed = microtime(true) - $start;

$sha1_start = microtime(true);
$hashB = hash('md5', $long_str);
$shal_elapsed = microtime(true) - $sha1_start;

echo "md5 took $elapsed seconds.
";
echo "hash took $shal_elapsed seconds.
";
?>

The idea of the code above is: to compare time in milliseconds before running the function against the time in milliseconds after running the function to see the elapsed time spent in the function itself.

The absolute time difference between md5( ) and hash( ) is on the order of a tenth of a millisecond. If you’re computing thousands or millions of hashes at a time, it makes sense to insert the extra runtime calculations that choose the fastest functions. But the fraction of a fraction of a breath of time saved in a handful of hash computations isn’t worth the extra complexity.



Similar Posts

Comments

Name (required)

Email (required)

Website

Speak your mind

Sponsors




Links