[英]Filter_var's performance
I read that filter_var
is not using regexp internally. 我读到
filter_var
不在内部使用regexp。 Does this mean that it is much faster than preg_match
? 这是否意味着它比
preg_match
快得多?
I want to check for email validation with speed performance in mind. 我想检查电子邮件验证是否考虑了速度性能。
Edit 编辑
This is not a duplicate : I am not asking the difference (as for what are the advantages of filter_var and preg_match), but which is faster to execute (with or without caching). 这不是重复的:我不是在问区别(关于filter_var和preg_match的优点是什么),但是执行起来更快(有或没有缓存)。 Besides, in the comments, it showed that filter_var uses regexp just like preg_match and this information was totally ignored in the related question.
此外,在注释中,它表明filter_var像preg_match一样使用regexp,并且此信息在相关问题中被完全忽略。
From a quick benchmark, it seems filter_var()
functions are slightly faster: 从快速基准测试来看,似乎
filter_var()
函数的速度稍快一些:
$times = 10000;
$result = array();
// preg_match version
// regex from https://github.com/php/php-src/blob/master/ext/filter/logical_filters.c#L525
$regexp = "/^(?!(?:(?:\\x22?\\x5C[\\x00-\\x7E]\\x22?)|(?:\\x22?[^\\x5C\\x22]\\x22?)){255,})(?!(?:(?:\\x22?\\x5C[\\x00-\\x7E]\\x22?)|(?:\\x22?[^\\x5C\\x22]\\x22?)){65,}@)(?:(?:[\\x21\\x23-\\x27\\x2A\\x2B\\x2D\\x2F-\\x39\\x3D\\x3F\\x5E-\\x7E]+)|(?:\\x22(?:[\\x01-\\x08\\x0B\\x0C\\x0E-\\x1F\\x21\\x23-\\x5B\\x5D-\\x7F]|(?:\\x5C[\\x00-\\x7F]))*\\x22))(?:\\.(?:(?:[\\x21\\x23-\\x27\\x2A\\x2B\\x2D\\x2F-\\x39\\x3D\\x3F\\x5E-\\x7E]+)|(?:\\x22(?:[\\x01-\\x08\\x0B\\x0C\\x0E-\\x1F\\x21\\x23-\\x5B\\x5D-\\x7F]|(?:\\x5C[\\x00-\\x7F]))*\\x22)))*@(?:(?:(?!.*[^.]{64,})(?:(?:(?:xn--)?[a-z0-9]+(?:-+[a-z0-9]+)*\\.){1,126}){1,}(?:(?:[a-z][a-z0-9]*)|(?:(?:xn--)[a-z0-9]+))(?:-+[a-z0-9]+)*)|(?:\\[(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){7})|(?:(?!(?:.*[a-f0-9][:\\]]){7,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?)))|(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){5}:)|(?:(?!(?:.*[a-f0-9]:){5,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3}:)?)))?(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))(?:\\.(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))){3}))\\]))$/iD";
$email = 'foo@bar.com';
$start_m = microtime();
$start = time();
for ($i=0; $i < $times; $i++){
preg_match($regexp, $email);
}
$end_m = microtime();
$end = time();
$bench = ($end - $start) + ($end_m - $start_m);
$result["preg_match"] = $bench;
// filter_var version
$start_m = microtime();
$start = time();
for ($i=0; $i < $times; $i++){
filter_var($email, FILTER_VALIDATE_EMAIL);
}
$end_m = microtime();
$end = time();
$bench = ($end - $start) + ($end_m - $start_m);
$result["filter_var"] = $bench;
print_r(
array(
"times" => $times,
"results" => $result
)
);
(code slightly modified version of this gist ) (对本要点进行略微修改的代码)
Result on my system: 结果在我的系统上:
PHP 5.5.9-1+sury.org~saucy+1 (cli) (built: Feb 13 2014 16:01:20)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies
with Zend OPcache v7.0.3, Copyright (c) 1999-2014, by Zend Technologies
Array
(
[times] => 10000
[results] => Array
(
[preg_match] => 0.16649
[filter_var] => 0.110912
)
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.