Skip to content

Instantly share code, notes, and snippets.

@Saeven
Created January 8, 2014 15:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Saeven/8318339 to your computer and use it in GitHub Desktop.
Save Saeven/8318339 to your computer and use it in GitHub Desktop.
Regex vs Filter/Count/Ctype
<?php
set_time_limit( 0 );
$searchText = 'Page and Brin originally nicknamed THEIR new search engine "BackRub", because the system checked backlinks to estimate the importance of a site.';
$start = microtime_float();
for( $i = 0 ; $i < 1000000 ; $i++ )
{
preg_match_all("/[A-Z]{1}[a-zA-z]*/", $searchText, $matches );
}
$total = microtime_float() - $start;
echo "Took $total seconds<br/>";
$str = $searchText;
$start = microtime_float();
for( $i = 0 ; $i < 1000000 ; $i++ )
{
$result = array_filter(str_word_count($str, 1), function($item) {
return ctype_upper($item[0]);
});
}
$total = microtime_float() - $start;
echo "Took $total seconds<br/>";
function microtime_float()
{
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment