PHP Benchmarks

Performance comparison of PHP code alternatives.

Test: preg_match vs. strpos (complex)

No Description

Run this test again Return to test menu

Historical Results

Based on (4) saved test runs, The test labeled "preg_match()" was (on average) the faster by 0.0109 seconds, (1.178% faster)

preg_match() 100%
strpos() 98.822%

The preg_match() test took 0.9168 seconds.
The strpos() test took 0.9277 seconds.

Nitty-Gritty

Each test case ran 20 random code order iterations consisting of 233,409 loops for a total of 4,668,175 runs.

  • Line execution difference (0.000002) milliseconds.
  • Avg difference (0.000) milliseconds per 233,409 loops.
  • Total difference 10.93 milliseconds for 4,668,175 loops

The iteration variablity for Code 1 was (0.0000) milliseconds and Code 2 was (0.0000) milliseconds. The lower and the closer together there values are the more accurate the results are.

Code

The first test, "preg_match()", was:

if (preg_match('~http://([^\./]+\.[^/]+?)/~', $GLOBALS['dummy'], $match) != 0)
	$GLOBALS['dummy2'] = $match[1];

The second test, "strpos()", was:

$pos_start = strpos($GLOBALS['dummy'], 'http://') + strlen('http://');
$pos_slash = strpos($GLOBALS['dummy'], '/', $pos_start + 2);

$pos_dot = strpos($GLOBALS['dummy'], '.', $pos_start + 1);
if ($pos_dot !== null && $pos_dot < $pos_slash)
	$GLOBALS['dummy2'] = substr($GLOBALS['dummy'], $pos_start, $pos_slash - $pos_start);

Running: Linux (x86_64:1 GB) PHP (7.2.15-1+ubuntu16.04.1+deb.sury.org+1)