Created
January 8, 2014 15:18
-
-
Save Saeven/8318339 to your computer and use it in GitHub Desktop.
Regex vs Filter/Count/Ctype
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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