PHP Benchmarks

Performance comparison of PHP code alternatives.

Test: preg_replace vs. preg_replace_callback

Although preg_replace with 'e' is depreciated, this is left for historical reasons

View test history (1) Run this test again Return to test menu

Result: Discarded

The test labeled "preg_replace()" was the faster by 0.0288 seconds, (4.973% faster)

preg_replace() 100%
preg_replace_callback() 95.027%

The preg_replace() test took 0.5497 seconds.
The preg_replace_callback() test took 0.5784 seconds.

Nitty-Gritty

Each test case ran 20 random code order iterations consisting of 131,710 loops for a total of 2,634,200 runs.

  • Line execution difference (0.000011) milliseconds.
  • Avg difference (1.438) milliseconds per 131,710 loops.
  • Total difference 28.77 milliseconds for 2,634,200 loops

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

Code

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

$GLOBALS['dummy2'] = preg_replace('~([Ww]heat|[Tt]hins)~e', "strtolower('$1')", $GLOBALS['dummy']);

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

/*
function cb($match)
{
	return strtolower($match[0]);
}
*/

$GLOBALS['dummy2'] = preg_replace_callback('~([Ww]heat|[Tt]hins)~', 'cb', $GLOBALS['dummy']);

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