Difference between revisions of "User:SignpostMarv Martin/is uuid comparison"
(adding code used for benchmarking) |
m (→benchmark: making it easier to add other funcs to the benchmark test) |
||
Line 130: | Line 130: | ||
<php> | <php> | ||
require_once('Benchmark/Iterate.php'); | require_once('Benchmark/Iterate.php'); | ||
function benchmark_is_uuid($string,$loop) | function benchmark_is_uuid($string,$loop,array $funcs) | ||
{ | { | ||
$results = array(); | $results = array(); | ||
$maxkeylen = null; | $maxkeylen = null; | ||
foreach($funcs as $func) | foreach($funcs as $func) | ||
Line 199: | Line 194: | ||
unset($results); | unset($results); | ||
} | } | ||
function batch_benchmark_is_uuid(array $strings,$loop) | function batch_benchmark_is_uuid(array $strings,$loop,array $funcs) | ||
{ | { | ||
foreach($strings as $string) | foreach($strings as $string) | ||
{ | { | ||
benchmark_is_uuid($string,$loop); | benchmark_is_uuid($string,$loop,$funcs); | ||
} | } | ||
} | } | ||
Line 218: | Line 213: | ||
': I thin-k I have it, but I\'m unsure', | ': I thin-k I have it, but I\'m unsure', | ||
), | ), | ||
10000 | 10000, | ||
array( | |||
'is_uuid_regex', | |||
'is_uuid_no_regex', | |||
'is_uuid_no_regex_ctype', | |||
) | |||
); | ); | ||
</php> | </php> | ||
</div> | </div> |
Revision as of 14:28, 29 July 2008
regex
<php> function is_uuid_regex($string) { return (bool)preg_match( '/^([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})$/S' ,$string); } </php>
no regex
<php> function is_uuid_no_regex($string) { $match = true; if(strlen($string) !== 36) { $match = false; } else if(strpos($string,'-') !== 8) { $match = false; } else if(strpos($string,'-',24) !== false) { $match = false; } else if(strpos($string,'-',9) !== 13) { $match = false; } else if(strpos($string,'-',13) !== 13) { $match = false; } else if(strpos($string,'-',14) !== 18) { $match = false; } else if(strpos($string,'-',18) !== 18) { $match = false; } else if(strpos($string,'-',19) !== 23) { $match = false; } else if(strpos($string,'-',23) !== 23) { $match = false; } else if(ctype_xdigit(str_replace('-',,$string)) === false) { $match = false; } return $match; } </php>
no regex or ctype
<php> function is_uuid_no_regex_ctype($string) { $match = true; if(strlen($string) !== 36) { $match = false; } else if(strpos($string,'-') !== 8) { $match = false; } else if(strpos($string,'-',24) !== false) { $match = false; } else if(strpos($string,'-',9) !== 13) { $match = false; } else if(strpos($string,'-',13) !== 13) { $match = false; } else if(strpos($string,'-',14) !== 18) { $match = false; } else if(strpos($string,'-',18) !== 18) { $match = false; } else if(strpos($string,'-',19) !== 23) { $match = false; } else if(strpos($string,'-',23) !== 23) { $match = false; } else if(($translated = strtr($string,array( '-'=>, '0'=>, '1'=>, '2'=>, '3'=>, '4'=>, '5'=>, '6'=>, '7'=>, '8'=>, '9'=>, 'a'=>, 'b'=>, 'c'=>, 'd'=>, 'e'=>, 'f'=>, ))) !== false && strlen($translated) !== 0) { $match = false; } return $match; } </php>
benchmark
<php> require_once('Benchmark/Iterate.php'); function benchmark_is_uuid($string,$loop,array $funcs) { $results = array(); $maxkeylen = null; foreach($funcs as $func) { $benchmark = new Benchmark_Iterate; $results[$func] = array( 'mean' => 0.0, 'common' => 0.0, 'shortest' => 0.0, 'longest' => 0.0, 'result' => null, ); if(isset($maxkeylen) === false) { foreach(array_keys($results[$func]) as $k) { $maxkeylen = (strlen($k) > $maxkeylen) ? strlen($k) : $maxkeylen; } } $results[$func]['result'] = call_user_func_array($func,array($string)) ? 'TRUE' : 'FALSE'; $benchmark->run($loop,$func,$string); $result = $benchmark->get(); unset($benchmark); $results[$func]['mean'] = $result['mean']; unset($result['mean'],$result['iterations']); $result_foo = array(); foreach($result as $v) { if(isset($result_foo[(string)$v]) === false) { $result_foo[(string)$v] = 0; } ++$result_foo[(string)$v]; } unset($result); arsort($result_foo); $results[$func]['common'] = key($result_foo); $times = array_keys($result_foo); unset($result_foo); sort($times); $results[$func]['shortest'] = current($times); $results[$func]['longest'] = end($times); unset($times); } echo $string; foreach($results as $func => $result) { echo "\n",$func; foreach($result as $k=>$v) { echo "\n\t",$k,': ',str_repeat(' ',$maxkeylen - strlen($k)),$v; } echo "\n\n"; } echo "\n"; unset($results); } function batch_benchmark_is_uuid(array $strings,$loop,array $funcs) { foreach($strings as $string) { benchmark_is_uuid($string,$loop,$funcs); } } ini_set('max_execution_time',0); define('NULL_KEY','00000000-0000-0000-0000-000000000000',true); header('Content-Type:text/plain'); batch_benchmark_is_uuid(array( NULL_KEY, '000000000000000000000000000000000000', '00000000-0000-0000-0000-00000000000#', '00000000-f000-0000-0000-000000000000', '00000000-z000-0000-0000-000000000000', '00000000--000-0000-0000-000000000000', ': I thin-k I have it, but I\'m unsure', ), 10000, array( 'is_uuid_regex', 'is_uuid_no_regex', 'is_uuid_no_regex_ctype', ) ); </php>