<html><head></head><body><?php //Test Harness for PHP performance. It handles up to three alternatives. //Just copy/paste your code to the lines that say "INSERT CODE TO BE //TESTED HERE," then upload to the server and surf to this web page. //Written by Warren Gaebel, B.A., B.C.S. //Released into public domain 2012.04.03. $numReps = 100; //adjust this value; 1,000,000 may work well $numberOfTests = 1; //adjust this value; 100 may work well $o1timeSum = 0; $o2timeSum = 0; $o3timeSum = 0; $timeDiff12 = 0; $timeDiff13 = 0; $timeDiff23 = 0; $timeDiff12Sum = 0; $timeDiff13Sum = 0; $timeDiff23Sum = 0; for ($j = $numberOfTests; $j--;) { echo ("<br />Test # " . ($numberOfTests - $j) . ":"); // OPTION #1 $o1start = time(); for ($i = $numReps; $i--;) { //INSERT CODE TO BE TESTED HERE - OPTION #1 } $o1stop = time(); $o1time = $o1stop - $o1start; $o1timeSum += $o1time; echo (" Option #1: " . $o1time); // OPTION #2 $o2start = time(); for ($i = $numReps; $i--;) { //INSERT CODE TO BE TESTED HERE - OPTION #2 } $o2stop = time(); $o2time = $o2stop - $o2start; $o2timeSum += $o2time; echo (" Option #2: " . $o2time); // OPTION #3 $o3start = time(); for ($i = $numReps; $i--;) { //INSERT CODE TO BE TESTED HERE - OPTION #3 } $o3stop = time(); $o3time = $o3stop - $o3start; $o3timeSum += $o3time; echo (" Option #3: " . $o3time); $timeDiff12Sum += ($timeDiff12 = $o1time - $o2time); $timeDiff13Sum += ($timeDiff13 = $o1time - $o3time); $timeDiff23Sum += ($timeDiff23 = $o2time - $o3time); } echo ("<br />"); echo ("<br />Average Time for Option 1: " . ($o1timeSum / $numberOfTests)); echo ("<br />Average Time for Option 2: " . ($o2timeSum / $numberOfTests)); echo ("<br />Average Time for Option 3: " . ($o3timeSum / $numberOfTests)); echo ("<br />"); echo ("<br />Time Difference (1-2): " . ($timeDiff12Sum / $numberOfTests)); echo ("<br />Time Difference (1-3): " . ($timeDiff13Sum / $numberOfTests)); echo ("<br />Time Difference (2-3): " . ($timeDiff23Sum / $numberOfTests)); ?></body></html>
It’s the same algorithm as in the JavaScript version, so the usage instructions and interpretation are the same. Please keep in mind the warnings in that last article, though. The PHP version is better because you can test your options on the production machine that will eventually run it, but it still suffers from the random effects of whatever happens to be running in the background. You may get significantly different results at peak time vs. quiet time, so don’t rely on only one or two tests.
These two test harnesses are quick and simple, but I’m already thinking of improvements to them. You can probably see some opportunities, too. It’s a good thing it’s in the public domain – you can customize it to your heart’s content.
I hope this helps.