Difference between revisions of "User:SignpostMarv Martin/is uuid comparison"
m (noticed ctype_xdigit was for hexadecimal-fu) |
(adding code used for benchmarking) |
||
Line 1: | Line 1: | ||
__TOC__ | |||
<div style="font-size:1.4em;"> | <div style="font-size:1.4em;"> | ||
== regex == | == regex == | ||
Line 126: | Line 127: | ||
</php> | </php> | ||
== benchmark == | |||
<php> | |||
require_once('Benchmark/Iterate.php'); | |||
function benchmark_is_uuid($string,$loop) | |||
{ | |||
$results = array(); | |||
$funcs = array( | |||
'is_uuid_regex', | |||
'is_uuid_no_regex', | |||
'is_uuid_no_regex_ctype', | |||
); | |||
$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) | |||
{ | |||
foreach($strings as $string) | |||
{ | |||
benchmark_is_uuid($string,$loop); | |||
} | |||
} | |||
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 | |||
); | |||
</php> | |||
</div> | </div> |
Revision as of 14:22, 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) { $results = array(); $funcs = array( 'is_uuid_regex', 'is_uuid_no_regex', 'is_uuid_no_regex_ctype', ); $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) { foreach($strings as $string) { benchmark_is_uuid($string,$loop); } } 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 ); </php>