Jcart wrote:ctype_alnum() is in all likelyhood faster than preg_*
Let's find out.
Code: Select all
<?php
$iterations = 10000;
$tests = array(
'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq' => true,
'This is exactly 64 bytes long, not counting the terminating byte' => false
);
$calls = array(
array('preg_match','#^[a-z0-9]*$#i'),
array('preg_match','#^[a-z0-9]+$#i'),
array('preg_match','#^[a-zA-Z0-9]*$#'),
array('preg_match','#^[A-z0-9]*$#'),
array('eregi','^[a-z0-9]*$'),
array('eregi','^[a-z0-9]+$'),
array('ereg','^[a-zA-Z0-9]*$'),
array('ereg','^[A-z0-9]*$'),
array('ctype_alnum'),
);
if(function_exists('bcadd'))
{
bcscale(20); // precision
function addTime(&$a,$b,$c)
{
$b = explode(' ', $b);
$c = explode(' ', $c);
$a = bcadd($a, bcadd(bcsub($c[0], $b[0]), bcsub($c[1], $b[1])));
}
function div($a, $b)
{
return bcdiv($a, $b);
}
}
else
{
function addTime(&$a,$b,$c)
{
$b = explode(' ', $b);
$c = explode(' ', $c);
$a += $c[0] - $b[0] + $c[1] - $b[1];
}
function div($a, $b)
{
return $a / $b;
}
}
function rendNumber($a)
{
$out = strval($a);
if(strpos('.',$out) !== false)
{
$out = rtrim($out, '.0');
}
if(func_num_args() > 1)
{
$arg = func_get_arg(1);
$out = number_format($out, $arg);
}
else
{
$out = number_format($out);
}
return $out;
}
function flash($var)
{
if($var === null)
{
return 'null';
}
elseif(is_array($var))
{
$c = 0;
$o = 'array(';
foreach($var as $k => $v)
{
$o .= ($c > 0 ? ',' : '') . ($k == $c ? '' : flash($k) . '=>') . flash($v);
$c++;
}
$o .= ')';
return $o;
}
elseif(is_scalar($var))
{
return var_export($var,true);
}
elseif(is_object($var))
{
return 'object ' . get_class($var);
}
elseif(is_resource($var))
{
return 'reference ' . get_resource_type($var);
}
else
{
return 'unknown';
}
}
$p = 8; // precision
$results = array();
foreach($tests as $test => $comp)
{
foreach($calls as $call)
{
$time = 0;
$func = array_shift($call);
array_push($call, $test);
for($i = 0; $i < $iterations; $i++)
{
$t1 = microtime();
$result = call_user_func_array($func, $call);
$t2 = microtime();
addTime($time, $t1, $t2);
}
$code = preg_replace('#^array(?=\()#i', $func, flash($call,true));
$pass = ($result == $comp ? 'PASS' : 'FAIL');
$result = flash($result);
array_push($results, array('code' => $code, 'result' => $result, 'pass' => $pass, 'time' => rendNumber($time, $p), 'avg' => rendNumber(div($time,$iterations), $p)));
}
}
$l = 0;
$c = strlen('Code');
$r = strlen('Result');
$p = strlen('Pass');
$t = strlen('Time');
$a = strlen('Average');
foreach($results as $result)
{
foreach($result as $key => $elem)
{
$$key = strlen($elem);
${$key{0}} = max($$key, ${$key{0}});
}
$l = max(2 + $code + 3 + $result + 3 + $pass + 3 + $time + 3 + $avg + 2, $l);
}
$line = '+' . str_repeat('-', $c + 2) . '+' . str_repeat('-', $r + 2) . '+' . str_repeat('-', $p + 2) . '+' . str_repeat('-', $t + 2) . '+' . str_repeat('-', $a + 2) . '+' . PHP_EOL;
echo rendNumber($iterations) . ' interation' . ($iterations != 1 ? 's' : '') . PHP_EOL;
echo $line;
printf("| %-{$c}s | %-{$r}s | %-{$p}s | %-{$t}s | %-{$a}s |" . PHP_EOL, 'Code', 'Result', 'Pass', 'Time', 'Average');
echo $line;
foreach($results as $result)
{
printf("| %-{$c}s | %{$r}s | %{$p}s | %{$t}s | %{$a}s |" . PHP_EOL, $result['code'], $result['result'], $result['pass'], $result['time'], $result['avg']);
}
echo $line;
?>
PHP 5.1.2
Code: Select all
10,000 interations
+---------------------------------------------------------------------------------------------------+--------+------+------------+------------+
| Code | Result | Pass | Time | Average |
+---------------------------------------------------------------------------------------------------+--------+------+------------+------------+
| preg_match('#^[a-z0-9]*$#i','abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq') | 1 | PASS | 0.11596100 | 0.00001160 |
| preg_match('#^[a-z0-9]+$#i','abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq') | 1 | PASS | 0.11462300 | 0.00001146 |
| preg_match('#^[a-zA-Z0-9]*$#','abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq') | 1 | PASS | 0.11865000 | 0.00001187 |
| preg_match('#^[A-z0-9]*$#','abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq') | 1 | PASS | 0.11458500 | 0.00001146 |
| eregi('^[a-z0-9]*$','abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq') | 1 | PASS | 0.15699600 | 0.00001570 |
| eregi('^[a-z0-9]+$','abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq') | 1 | PASS | 0.14902200 | 0.00001490 |
| ereg('^[a-zA-Z0-9]*$','abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq') | 1 | PASS | 0.15475600 | 0.00001548 |
| ereg('^[A-z0-9]*$','abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq') | 1 | PASS | 0.15353700 | 0.00001535 |
| ctype_alnum('abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq') | true | PASS | 0.09177500 | 0.00000918 |
| preg_match('#^[a-z0-9]*$#i','This is exactly 64 bytes long, not counting the terminating byte') | 0 | PASS | 0.12162800 | 0.00001216 |
| preg_match('#^[a-z0-9]+$#i','This is exactly 64 bytes long, not counting the terminating byte') | 0 | PASS | 0.11854600 | 0.00001185 |
| preg_match('#^[a-zA-Z0-9]*$#','This is exactly 64 bytes long, not counting the terminating byte') | 0 | PASS | 0.12182100 | 0.00001218 |
| preg_match('#^[A-z0-9]*$#','This is exactly 64 bytes long, not counting the terminating byte') | 0 | PASS | 0.12280300 | 0.00001228 |
| eregi('^[a-z0-9]*$','This is exactly 64 bytes long, not counting the terminating byte') | false | PASS | 0.16258800 | 0.00001626 |
| eregi('^[a-z0-9]+$','This is exactly 64 bytes long, not counting the terminating byte') | false | PASS | 0.14989100 | 0.00001499 |
| ereg('^[a-zA-Z0-9]*$','This is exactly 64 bytes long, not counting the terminating byte') | false | PASS | 0.16287400 | 0.00001629 |
| ereg('^[A-z0-9]*$','This is exactly 64 bytes long, not counting the terminating byte') | false | PASS | 0.16153800 | 0.00001615 |
| ctype_alnum('This is exactly 64 bytes long, not counting the terminating byte') | false | PASS | 0.08918900 | 0.00000892 |
+---------------------------------------------------------------------------------------------------+--------+------+------------+------------+
PHP 4.4.1
Code: Select all
10,000 interations
+---------------------------------------------------------------------------------------------------+--------+------+------------+------------+
| Code | Result | Pass | Time | Average |
+---------------------------------------------------------------------------------------------------+--------+------+------------+------------+
| preg_match('#^[a-z0-9]*$#i','abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq') | 1 | PASS | 0.06220700 | 0.00000622 |
| preg_match('#^[a-z0-9]+$#i','abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq') | 1 | PASS | 0.06118600 | 0.00000612 |
| preg_match('#^[a-zA-Z0-9]*$#','abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq') | 1 | PASS | 0.06278200 | 0.00000628 |
| preg_match('#^[A-z0-9]*$#','abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq') | 1 | PASS | 0.06250400 | 0.00000625 |
| eregi('^[a-z0-9]*$','abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq') | 1 | PASS | 0.11453300 | 0.00001145 |
| eregi('^[a-z0-9]+$','abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq') | 1 | PASS | 0.10529900 | 0.00001053 |
| ereg('^[a-zA-Z0-9]*$','abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq') | 1 | PASS | 0.11655600 | 0.00001166 |
| ereg('^[A-z0-9]*$','abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq') | 1 | PASS | 0.11385300 | 0.00001139 |
| ctype_alnum('abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq') | true | PASS | 0.05107100 | 0.00000511 |
| preg_match('#^[a-z0-9]*$#i','This is exactly 64 bytes long, not counting the terminating byte') | 0 | PASS | 1.06066100 | 0.00010607 |
| preg_match('#^[a-z0-9]+$#i','This is exactly 64 bytes long, not counting the terminating byte') | 0 | PASS | 0.06009300 | 0.00000601 |
| preg_match('#^[a-zA-Z0-9]*$#','This is exactly 64 bytes long, not counting the terminating byte') | 0 | PASS | 0.06421300 | 0.00000642 |
| preg_match('#^[A-z0-9]*$#','This is exactly 64 bytes long, not counting the terminating byte') | 0 | PASS | 0.06346500 | 0.00000635 |
| eregi('^[a-z0-9]*$','This is exactly 64 bytes long, not counting the terminating byte') | false | PASS | 0.12100700 | 0.00001210 |
| eregi('^[a-z0-9]+$','This is exactly 64 bytes long, not counting the terminating byte') | false | PASS | 1.10673000 | 0.00011067 |
| ereg('^[a-zA-Z0-9]*$','This is exactly 64 bytes long, not counting the terminating byte') | false | PASS | 0.12095500 | 0.00001210 |
| ereg('^[A-z0-9]*$','This is exactly 64 bytes long, not counting the terminating byte') | false | PASS | 0.12074500 | 0.00001207 |
| ctype_alnum('This is exactly 64 bytes long, not counting the terminating byte') | false | PASS | 0.04778800 | 0.00000478 |
+---------------------------------------------------------------------------------------------------+--------+------+------------+------------+