简体   繁体   中英

Filter_var's performance

I read that filter_var is not using regexp internally. Does this mean that it is much faster than 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). 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.

From a quick benchmark, it seems filter_var() functions are slightly faster:

$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
        )

)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM