Skip to content

Instantly share code, notes, and snippets.

@joshhartman
Created February 28, 2011 00:08
Show Gist options
  • Save joshhartman/846721 to your computer and use it in GitHub Desktop.
Save joshhartman/846721 to your computer and use it in GitHub Desktop.
Get File Extension PHP Benchmark
<?php
$file = 'c:\\xampplite\\htdocs\\index.php';
$max = 1000000;
?>
<p>Command: <strong>$ext = end(explode('.', $file));</strong></p>
<?php
// Start TIMER
$stimer = explode( ' ', microtime() );
$stimer = $stimer[1] + $stimer[0];
?>
<?php for($x=0; $x<$max; $x++){ ?>
<?php $ext = end(explode('.', $file)); echo ($x==0)?'Result: '.$ext:''; ?>
<?php } ?>
<?php
// End TIMER
$etimer = explode( ' ', microtime() );
$etimer = $etimer[1] + $etimer[0];
echo '<p>';
printf( "Execution Time: <b>%f</b> seconds.", ($etimer-$stimer) );
echo '</p>';
?>
<p>&nbsp;</p>
<p>Command: <strong>$ext = substr($file, strrpos($file, '.')+1);</strong></p>
<?php
// Start TIMER
$stimer = explode( ' ', microtime() );
$stimer = $stimer[1] + $stimer[0];
?>
<?php for($x=0; $x<$max; $x++){ ?>
<?php $ext = substr($file, strrpos($file, '.')+1); echo ($x==0)?'Result: '.$ext:''; ?>
<?php } ?>
<?php
// End TIMER
$etimer = explode( ' ', microtime() );
$etimer = $etimer[1] + $etimer[0];
echo '<p>';
printf( "Execution Time: <b>%f</b> seconds.", ($etimer-$stimer) );
echo '</p>';
?>
<p>&nbsp;</p>
<p>Command: <strong>$ext = pathinfo($file, PATHINFO_EXTENSION);</strong></p>
<?php
// Start TIMER
$stimer = explode( ' ', microtime() );
$stimer = $stimer[1] + $stimer[0];
?>
<?php for($x=0; $x<$max; $x++){ ?>
<?php $ext = pathinfo($file, PATHINFO_EXTENSION); echo ($x==0)?'Result: '.$ext:''; ?>
<?php } ?>
<?php
// End TIMER
$etimer = explode( ' ', microtime() );
$etimer = $etimer[1] + $etimer[0];
echo '<p>';
printf( "Execution Time: <b>%f</b> seconds.", ($etimer-$stimer) );
echo '</p>';
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment