Advertisement
Guest User

Untitled

a guest
Aug 15th, 2013
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.14 KB | None | 0 0
  1. $times = 100000;
  2.  
  3. $result = array();
  4.  
  5. // preg_match
  6. // regexp is stolen from:
  7. // https://github.com/php/php-src/blob/master/ext/filter/logical_filters.c#L525
  8. $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";
  9.  
  10. $ip4 = '123.123.123.123';
  11. $ip6 = '2001:0db8:85a3:0042:1000:8a2e:0370:7334';
  12.  
  13.  
  14. $start_m = microtime();
  15. $start = time();
  16. for ($i=0; $i < $times; $i++){
  17.       preg_match($regexp, $ip4);
  18.       preg_match($regexp, $ip6);
  19. }  
  20.  
  21. $end_m = microtime();
  22. $end = time();
  23. $bench = ($end - $start) + ($end_m - $start_m);
  24. $result["preg_match"] = $bench;
  25.  
  26. // filter_var
  27. $start_m = microtime();
  28. $start = time();
  29. for ($i=0; $i < $times; $i++){
  30.       filter_var($ip6, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6);
  31.       filter_var($ip4, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6);
  32. }
  33.  
  34. $end_m = microtime();
  35. $end = time();
  36. $bench = ($end - $start) + ($end_m - $start_m);
  37. $result["filter_var"] = $bench;
  38.  
  39. var_dump(array(
  40.   "times" => $times,
  41.   "results" => $result
  42. ));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement