Skip to content

Instantly share code, notes, and snippets.

@GaryJones
Created September 27, 2011 11:38
Show Gist options
  • Save GaryJones/1244864 to your computer and use it in GitHub Desktop.
Save GaryJones/1244864 to your computer and use it in GitHub Desktop.
Increment Test
<?php
ini_set( 'MAX_EXEC_TIME', 120 );
ob_start();
$num_tests = 20;
$num_loops = 1000000;
$the_tests = array(
'++$i',
'$i += 1',
'$i++',
'$i = $i + 1',
);
for ( $j = 0; $j < $num_tests; ++$j ) {
// ++$i
for ( $i = 0, $start = microtime( true ); $i < $num_loops; ++$i )
$someval = 2;
$results[0][$j] = microtime( true ) - $start;
// $i += 1
for ( $i = 0, $start = microtime( true ); $i < $num_loops; $i += 1 )
$someval = 2;
$results[1][$j] = microtime( true ) - $start;
// $i++
for ( $i = 0, $start = microtime( true ); $i < $num_loops; $i++ )
$someval = 2;
$results[2][$j] = microtime( true ) - $start;
// $i = $i + 1
for ( $i = 0, $start = microtime( true ); $i < $num_loops; $i = $i + 1 )
$someval = 2;
$results[3][$j] = microtime( true ) - $start;
}
ob_end_flush();
?>
<html>
<head>
<title>Increment Test</title>
</head>
<body>
<h1>Increment Test</h1>
<p><a href="https://gist.github.com/1244864">Source</a></p>
<?php
foreach( $results as $result => $tests ) {
$total = 0;
echo '<div style="width: 200px; float: left;"><h2>' . $the_tests[$result] . '</h2>';
echo '<table><thead><th>Time (s)</th></thead>';
echo '<tbody>';
foreach( $tests as $run ) {
echo '<tr><td>' . $run . '</td></tr>';
$total += $run;
}
echo '<tr><td><strong>' . $total / $num_tests . '</strong></td></tr>';
echo '</tbody></table></div>';
}
?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment