Skip to content

Instantly share code, notes, and snippets.

@mattonomics
Created February 27, 2014 17:23
Show Gist options
  • Save mattonomics/9254740 to your computer and use it in GitHub Desktop.
Save mattonomics/9254740 to your computer and use it in GitHub Desktop.
<?php
// First, we have the strings to test.
$string_to_find = 'brisket';
$string_to_search = '5rWIZiWxvRAqrKEJgx8IS0JZkKxop9Z5rWg7yPZsU8zzBneMR65tgZzPd5LZtfUBNbKpthEI4qSZJC6H3yFCpl3VaA3aYbrisket';
// now we'll test strpos…
echo 'Start time, strpos(): ', ( $strpos_start = microtime( true ) ), "\n\n";
for ( $i = 1; $i <= 1000000; $i++ ) {
if ( strpos( $string_to_search, $string_to_find ) ) {
}
}
echo 'End time strpos(): ', ( $strpos_end = microtime( true ) ), "\n\n";
echo "Difference: ", ( $strpos_end - $strpos_start ), "\n\n/*************/\n\n";
// …and stripos…
echo 'Start time, stripos(): ', ( $stripos_start = microtime( true ) ), "\n\n";
for ( $i = 1; $i <= 1000000; $i++ ) {
if ( stripos( $string_to_search, $string_to_find ) ) {
}
}
echo 'End time stripos(): ', ( $stripos_end = microtime( true ) ), "\n\n";
echo "Difference: ", ( $stripos_end - $stripos_start ), "\n\n/*************/\n\n";
// …and preg_match…
echo 'Start time, preg_match(): ', ( $preg_match_start = microtime( true ) ), "\n\n";
for ( $i = 1; $i <= 1000000; $i++ ) {
if ( preg_match( "/$string_to_find/", $string_to_search ) ) {
}
}
echo 'End time preg_match(): ', ( $preg_match_end = microtime( true ) ), "\n\n";
echo "Difference: ", ( $preg_match_end - $preg_match_start ), "\n\n/*************/\n\n";
// …and strstr…
echo 'Start time, strstr(): ', ( $strstr_start = microtime( true ) ), "\n\n";
for ( $i = 1; $i <= 1000000; $i++ ) {
if ( strstr( $string_to_search, $string_to_find ) ) {
}
}
echo 'End time strstr(): ', ( $strstr_end = microtime( true ) ), "\n\n";
echo "Difference: ", ( $strstr_end - $strstr_start ), "\n\n/*************/\n\n";
// …and stristr…
echo 'Start time, stristr(): ', ( $stristr_start = microtime( true ) ), "\n\n";
for ( $i = 1; $i <= 1000000; $i++ ) {
if ( stristr( $string_to_search, $string_to_find ) ) {
}
}
echo 'End time stristr(): ', ( $stristr_end = microtime( true ) ), "\n\n";
echo "Difference: ", ( $stristr_end - $stristr_start ), "\n\n/*************/\n\n";
// now let's see which is faster: echo $a, $b __or__ echo $a . $b
echo 'Start time, echo with ",": ', ( $echo_start = microtime( true ) ), "\n\n";
ob_start();
for ( $j = 0; $j <= 1000000; $j++ ) {
echo $string_to_find, $string_to_search;
}
ob_end_clean();
echo 'End time echo with ",": ', ( $echo_end = microtime( true ) ), "\n\n";
echo "Difference: ", ( $echo_end - $echo_start ), "\n\n/*************/\n\n";
echo 'Start time, echo with ".": ', ( $echo_d_start = microtime( true ) ), "\n\n";
ob_start();
for ( $j = 0; $j <= 1000000; $j++ ) {
echo $string_to_find . $string_to_search;
}
ob_end_clean();
echo 'End time echo with ".": ', ( $echo_d_end = microtime( true ) ), "\n\n";
echo "Difference: ", ( $echo_d_end - $echo_d_start ), "\n\n/*************/\n\n";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment