Difference between revisions of "User:SignpostMarv Martin/is uuid comparison"
(PHP-fu) |
|||
(4 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
__TOC__ | |||
<div style="font-size:1.4em;"> | <div style="font-size:1.4em;"> | ||
== regex == | == regex == | ||
Line 5: | Line 6: | ||
{ | { | ||
return (bool)preg_match( | return (bool)preg_match( | ||
'/^([0-9a-f]{8}-[0-9a- | '/^([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})$/S' | ||
,$string); | ,$string); | ||
} | |||
</php> | |||
== ctype_xdigit function == | |||
<php> | |||
if(function_exists('ctype_xdigit') === false) | |||
{ | |||
function ctype_xdigit($string) | |||
{ | |||
return (($translated = strtr($string,array( | |||
'0'=>'', | |||
'1'=>'', | |||
'2'=>'', | |||
'3'=>'', | |||
'4'=>'', | |||
'5'=>'', | |||
'6'=>'', | |||
'7'=>'', | |||
'8'=>'', | |||
'9'=>'', | |||
'a'=>'', | |||
'b'=>'', | |||
'c'=>'', | |||
'd'=>'', | |||
'e'=>'', | |||
'f'=>'', | |||
))) !== false && strlen($translated) !== 0); | |||
} | |||
} | } | ||
</php> | </php> | ||
Line 12: | Line 41: | ||
== no regex == | == no regex == | ||
<php> | <php> | ||
function | function is_uuid_SignpostMarv_Martin($string) | ||
{ | { | ||
$match = true; | $match = true; | ||
Line 51: | Line 80: | ||
$match = false; | $match = false; | ||
} | } | ||
else if( | else if(ctype_xdigit(str_replace('-','',$string)) === false) | ||
{ | { | ||
$match = false; | $match = false; | ||
Line 81: | Line 88: | ||
</php> | </php> | ||
== | === nested ternary === | ||
<php> | <php> | ||
function | function is_uuid_nested_ternary_SignpostMarv_Martin($string) | ||
{ | { | ||
return (strlen($string) !== 36) ? | |||
false : | |||
( | |||
(strpos($string,'-') !== 8) ? | |||
false : | |||
( | |||
(strpos($string,'-',24) !== false) ? | |||
false : | |||
( | |||
(strpos($string,'-',9) !== 13) ? | |||
false : | |||
( | |||
(strpos($string,'-',13) !== 13) ? | |||
false : | |||
( | |||
(strpos($string,'-',14) !== 18) ? | |||
false : | |||
( | |||
(strpos($string,'-',18) !== 18) ? | |||
false : | |||
( | |||
(strpos($string,'-',19) !== 23) ? | |||
false : | |||
( | |||
(strpos($string,'-',23) !== 23) ? | |||
false : | |||
ctype_xdigit(str_replace('-','',$string)) | |||
) | |||
) | |||
) | |||
) | |||
) | |||
) | |||
) | |||
) | |||
; | |||
} | |||
</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_nested_ternary_SignpostMarv_Martin', | |||
'is_uuid_nested_ternary_SignpostMarv_Martin', | |||
) | |||
); | |||
</php> | </php> | ||
</div> | </div> |
Latest revision as of 15:15, 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>
ctype_xdigit function
<php> if(function_exists('ctype_xdigit') === false) { function ctype_xdigit($string) { return (($translated = strtr($string,array( '0'=>, '1'=>, '2'=>, '3'=>, '4'=>, '5'=>, '6'=>, '7'=>, '8'=>, '9'=>, 'a'=>, 'b'=>, 'c'=>, 'd'=>, 'e'=>, 'f'=>, ))) !== false && strlen($translated) !== 0); } } </php>
no regex
<php> function is_uuid_SignpostMarv_Martin($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>
nested ternary
<php> function is_uuid_nested_ternary_SignpostMarv_Martin($string) {
return (strlen($string) !== 36) ? false : ( (strpos($string,'-') !== 8) ? false : ( (strpos($string,'-',24) !== false) ? false : ( (strpos($string,'-',9) !== 13) ? false : ( (strpos($string,'-',13) !== 13) ? false : ( (strpos($string,'-',14) !== 18) ? false : ( (strpos($string,'-',18) !== 18) ? false : ( (strpos($string,'-',19) !== 23) ? false : ( (strpos($string,'-',23) !== 23) ? false : ctype_xdigit(str_replace('-',,$string)) ) ) ) ) ) ) ) ) ;
} </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_nested_ternary_SignpostMarv_Martin', 'is_uuid_nested_ternary_SignpostMarv_Martin', ) ); </php>