Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $times = 100000;
- $result = array();
- // preg_match
- // regexp is stolen from:
- // https://github.com/php/php-src/blob/master/ext/filter/logical_filters.c#L525
- $regexp = "/^(((([1]?\d)?\d|2[0-4]\d|25[0-5])\.){3}(([1]?\d)?\d|2[0-4]\d|25[0-5]))|([\da-fA-F]{1,4}(\:[\da-fA-F]{1,4}){7})|(([\da-fA-F]{1,4}:){0,5}::([\da-fA-F]{1,4}:){0,5}[\da-fA-F]{1,4})$/iD";
- $ip4 = '123.123.123.123';
- $ip6 = '2001:0db8:85a3:0042:1000:8a2e:0370:7334';
- $start_m = microtime();
- $start = time();
- for ($i=0; $i < $times; $i++){
- preg_match($regexp, $ip4);
- preg_match($regexp, $ip6);
- }
- $end_m = microtime();
- $end = time();
- $bench = ($end - $start) + ($end_m - $start_m);
- $result["preg_match"] = $bench;
- // filter_var
- $start_m = microtime();
- $start = time();
- for ($i=0; $i < $times; $i++){
- filter_var($ip6, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6);
- filter_var($ip4, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6);
- }
- $end_m = microtime();
- $end = time();
- $bench = ($end - $start) + ($end_m - $start_m);
- $result["filter_var"] = $bench;
- var_dump(array(
- "times" => $times,
- "results" => $result
- ));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement