@ 2024-01-03T14:58:24Z <?php
$urls = [
'absolute' => 'https://stackoverflow.com/questions/20248666/in-this-case-which-is-better-in-matching-regular-expression-or-parse-url',
'relative' => '/questions/20248666/in-this-case-which-is-better-in-matching-regular-expression-or-parse-url',
'absolute-with-query' => 'https://stackoverflow.com/questions/20248666/in-this-case-which-is-better-in-matching-regular-expression-or-parse-url?r=https://3v4l.org',
'relative-with-query' => '/questions/20248666/in-this-case-which-is-better-in-matching-regular-expression-or-parse-url?r=https://3v4l.org',
];
$parseTester = function ($url) {
$urlComponents = parse_url($url, PHP_URL_SCHEME);
return strncmp($url, '//', 2) && empty($urlComponents);
};
$regexpTester = function ($url) {
return preg_match('@^((\w+):)?//@', $url);
};
$strposTester = function ($url) {
return strpos($url, 'https://') !== 0
|| strpos($url, 'http://') !== 0
|| strpos($url, '//') !== 0;
};
$strposAlternativeTester = function ($url) {
if (strncmp($url, '//', 2) === 0) {
return false;
}
$pos = strpos($url, '://');
if ($pos === false) {
return true;
}
$queryPos = strpos($url, '?');
return $queryPos === false || $pos < $queryPos;
};
$test = function ($testerName, $callback) use ($urls) {
foreach ($urls as $scenario => $url) {
$time = microtime(true);
for ($i = 0; $i < 1000000; $i++) {
$callback($url);
}
echo "$testerName-$scenario: ", number_format(microtime(true) - $time, 10), "\n";
}
echo "\n";
};
$test('parseTester', $parseTester);
$test('regexpTester', $regexpTester);
$test('strposTester', $strposTester);
$test('strposAlternativeTester', $strposAlternativeTester);
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).
Version System time (s) User time (s) Memory (MiB) 8.3.1 0.006 1.547 22.64 8.3.0 0.003 1.709 19.13 8.2.14 0.013 1.731 22.27 8.2.13 0.007 1.581 20.64 8.2.12 0.007 1.591 19.22 8.2.11 0.003 1.497 19.25 8.2.10 0.003 1.544 19.10 8.2.9 0.007 1.526 19.14 8.2.8 0.010 1.487 19.28 8.2.7 0.013 1.566 20.81 8.2.6 0.023 1.602 20.75 8.2.5 0.017 1.530 19.03 8.2.4 0.013 1.549 18.90 8.2.3 0.003 1.542 18.95 8.2.2 0.007 1.550 20.54 8.2.1 0.000 1.520 20.76 8.2.0 0.007 1.576 20.60 8.1.27 0.013 1.554 24.01 8.1.26 0.007 1.567 22.16 8.1.25 0.013 1.582 22.39 8.1.24 0.010 1.509 18.70 8.1.23 0.003 1.559 18.79 8.1.22 0.003 1.603 19.12 8.1.21 0.013 1.563 20.48 8.1.20 0.013 1.603 20.46 8.1.19 0.003 1.597 20.33 8.1.18 0.010 1.537 18.55 8.1.17 0.010 1.519 18.87 8.1.16 0.007 1.489 18.86 8.1.15 0.017 1.570 18.74 8.1.14 0.003 1.628 18.64 8.1.13 0.013 1.595 18.82 8.1.12 0.013 1.594 18.66 8.1.11 0.007 1.563 18.88 8.1.10 0.000 1.560 20.41 8.1.9 0.003 1.575 18.88 8.1.8 0.003 1.584 18.80 8.1.7 0.010 1.541 18.79 8.1.6 0.007 1.534 19.01 8.1.5 0.007 1.548 18.88 8.1.4 0.007 1.573 19.06 8.1.3 0.010 1.553 18.89 8.1.2 0.007 1.533 18.99 8.1.1 0.013 1.572 19.07 8.1.0 0.003 1.774 18.86
preferences:dark mode live preview ace vim emacs key bindings
42.67 ms | 403 KiB | 5 Q