Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I need to test performance of a function in PHP. This has to happen accurately, of course. In the end I need to know how long it takes for a function to perform. What I'm doing now:

<?php
$start = microtime(true);
while (microtime(true) != $start + 1); // Wait a second to start up

$loops = 10;
$avg = 0;
for ($i = 0; $i < $loops; $i++) {
    $start = microtime(true);
    for ($c = 0; $c < 1000; $c++) {    // the 1000 may vary depending on the function
        theFunction();
    }
    $end = microtime(true);

    echo "Take $i: " . ($end-$start) . "s\n";

    $avg *= $i;
    $avg += $end - $start;
    $avg /= $i + 1;
}

echo "Average: " . $avg . "s\n";
?>

Is this a good way to test performance of a function in PHP? It returns about the same result all the time, within a margin of ~5% when the server does nothing else.

Let's assume the environment is clean (i.e. the server doesn't have to do anything else in the meanwhile), would this code be good for testing, or does it count too much or too little?

share|improve this question
add comment

2 Answers

I don't see any immediate problems with it, but I would probably simplified it a bit. Not sure why you're doing it in two loops like that, but I might be missing something of course :)

<?php
$loops = 10000;

$start = microtime(true);

for ($i = 0; $i < $loops; $i++)
    theFunction();

$total = microtime(true) - $start;
$avg = $total / $loops;

echo "Average: " . $avg . "s\n";
share|improve this answer
 
But now the loop takes time too! :) I have the two loops to check if something else happens on the server, which might give an extreme. –  Camil Staps Jun 6 '13 at 8:45
 
The loop takes time? The for statement itself should take close to zero amount of time compared to your function. How are you checking if something else happens on the server exactly? The only technical difference I see between these two is that yours does more arithmetic. –  Svish Jun 6 '13 at 8:48
 
It would depend on the function how much time the loop takes. If you're comparing, say, single or double quotation marks, this might give problems. When you see all results like 50ms and one like 500ms, you'd know that line isn't trustworthy. –  Camil Staps Jun 6 '13 at 8:50
1  
If you're comparing things like singe vs double quotation marks I'd recommend you spend your time on something else :p Seriously, the for loop really shouldn't matter. If you're looking for speeds requiring this level of optimization you'd probably be better off using a different language than PHP. –  Svish Jun 6 '13 at 9:58
 
I wrote a class that will take care of the timing for you: (github.com/jsanc623/PHPBenchTime). Will add an answer below using my benchmark timer class mixed with the answer from @Svish –  jsanc623 Jun 11 '13 at 14:31
add comment

Extending Svish's answer to utilize my PHP benchmark timer class:

<?php
require('PHPBenchTime.php');
use PHPBenchTime\Timer as Timer;
$Benchmark = new Timer;

$Benchmark->Start();

for ($i = 0; $i < 10000; $i++){
    theFunction();
 }

$time = $Benchmark->End();

echo "Total: " . $time['Total'] . "s\n";
echo "Average: " . $time['Total'] / 10000 . "s\n";

or, using named laps:

<?php
require('PHPBenchTime.php');
use PHPBenchTime\Timer as Timer;
$Benchmark = new Timer;

$Benchmark->Start();

for ($i = 0; $i < 10000; $i++){
    theFunction();
    $Benchmark->Lap("Lap #" . $i);
 }

$time = $Benchmark->End();

echo "Total: " . $time['Total'] . "s\n";
echo "Average: " . $time['Total'] / 10000 . "s\n";
echo "Laps: \n";
foreach($time['Laps'] as $key => $value){
    echo $key . " " . $value . "s\n";
}
share|improve this answer
 
Won't these method calls and stuff kind of affect the timing though? At least considering he's worried about the for loop... –  Svish Jun 11 '13 at 19:28
 
The second example (with the named laps) will affect timing, but not that greatly. I've had success with 250k loops and a deviation of about a quarter to a half a second. The first sample without the named laps will not affect timing. But like you said - if he's worried about the for loop or the performance gains between using single and double quotes, he's better off looking at other avenues of optimization (language, stack, jit, hardware, refactoring, etc) –  jsanc623 Jun 11 '13 at 20:40
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.