PHP code speed test
Compare the speeds of two different code snippets
I wanted to compare the speed of PHP 'echo' with a function that does nothing except 'echo'. A code snippet can be tested by looping through it (repeating it) a large number of times and measuring the time difference between the start of the loop and when the loop has finished looping. By repeating the test in a browser it's easy to find a general average for both options.
The function being tested:
// Function name: '_print' function _print($text) { echo $text; } // To compare: _print($string); // with: echo $string;
The PHP script used for the test:
<?php error_reporting(E_ALL); // Conditions $string = '. '; $iterations = 100000; // Number of loops /* ---------------------------------------- */ /* First test: echo */ // START OF TIME MEASUREMENT $tm_start1 = 0; $tm_start1 = array_sum(explode(' ', microtime())); echo '<!-- '; for ($i = 0; $i < $iterations; $i++) { // Loop through // To test, do something 100,000 times echo $string; } echo ' //-->'; // END OF TIME MEASUREMENT $execution_time = 0; // Initiate execution time // Get the interval in seconds to 6 decimal places $execution_time = (array_sum(explode(' ', microtime())) - $tm_start1); $execution_time = number_format($execution_time, 6); echo "\n\n"; echo '<br>Execution time for echo = ' . $execution_time . ' seconds'; echo "\n\n"; /* ---------------------------------------- */ /* Second test: _print() function */ // Function function _print($text) { echo $text; } // START OF TIME MEASUREMENT $tm_start2 = 0; $tm_start2 = array_sum(explode(' ', microtime())); echo '<!-- '; for ($i = 0; $i < $iterations; $i++) { // Loop through // To test, do something 100,000 times _print($string); } echo ' //-->'; // END OF TIME MEASUREMENT $execution_time = 0; // Initiate execution time // Get the interval in seconds to 6 decimal places $execution_time = (array_sum(explode(' ', microtime())) - $tm_start2); $execution_time = number_format($execution_time, 6); echo "\n\n"; echo '<br>Execution time for _print() = ' . $execution_time . ' seconds'; echo "\n\n"; ?>
Both options should be tested in the same request for a fair comparison. The script sends text to the browser (200,000 dots and 200,000 spaces) but for convenience they are hidden inside HTML comments so all you see is the results.
Results
Execution time for echo = .005150 seconds
Execution time for _print() = .007816 seconds
Normal echo is faster – only 65% of the time taken by the function _print() doing exactly the same thing. Having said that, seven thousandths of a second (7 milliseconds) is incredibly fast for 100,000 iterations so it doesn't make any practical difference which is used. The main thing it shows is that putting something in a function is a tiny bit slower.
To put the 7 milliseconds in context, a request for the above script on my shared server typically includes 58 milliseconds for the initial connection and 108 milliseconds 'Waiting (TTFB)' (time to first byte).
- You last viewed:
- janet-donnison
- | houben-lejeune
- | war-diary-10