Skip to content

Instantly share code, notes, and snippets.

@aeno
Created June 23, 2020 09:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aeno/663607f1243ed2f8fcbda4446b600b00 to your computer and use it in GitHub Desktop.
Save aeno/663607f1243ed2f8fcbda4446b600b00 to your computer and use it in GitHub Desktop.
Quick and dirty PHP benchmarking
<?php
/**
* Very simple PHP benchmarking.
*
* @noinspection PhpUnused
*/
// name your contestants
const NAME1 = 'explode()';
const NAME2 = 'preg_split()';
// set some shared data to work with
const TEXT = <<<EOL
God, ooh. Sunny, gutless lasses quirky fear a misty, mighty lagoon.
Riddle ho! ransack to be haulled.
Golly gosh, never haul a pegleg.
The real anchor fiery burns the biscuit eater.
God, ooh. Sunny, gutless lasses quirky fear a misty, mighty lagoon.
Riddle ho! ransack to be haulled.
Golly gosh, never haul a pegleg.
The real anchor fiery burns the biscuit eater.
God, ooh. Sunny, gutless lasses quirky fear a misty, mighty lagoon.
Riddle ho! ransack to be haulled.
Golly gosh, never haul a pegleg.
The real anchor fiery burns the biscuit eater.
God, ooh. Sunny, gutless lasses quirky fear a misty, mighty lagoon.
Riddle ho! ransack to be haulled.
Golly gosh, never haul a pegleg.
The real anchor fiery burns the biscuit eater.
EOL;
// how many iterations to test
const ITERATIONS = 1000000;
/**
* First contestant
*/
function test1()
{
explode("\n", TEXT);
}
/**
* Second contestant
*/
function test2()
{
preg_split("/\n/", TEXT);
}
//
// END OF CONFIG #######################################################################################################
//
// these three constants are emojis and may be invisible in your editor:
const FIRST_PLACE = '🥇';
const SECOND_PLACE = '🥈';
const THIRD_PLACE = '🥉';
echo sprintf(
"benchmarking %s iterations on PHP %s..." . PHP_EOL . PHP_EOL,
number_format(ITERATIONS, 0, ',', '.'),
PHP_VERSION
);
// benchmark the first contestant
$start = microtime(true);
for ($i = 0; $i <= ITERATIONS; $i++) {
test1();
}
$dur1 = microtime(true) - $start;
// benchmark the second contestant
$start = microtime(true);
for ($i = 0; $i <= ITERATIONS; $i++) {
test2();
}
$dur2 = microtime(true) - $start;
// calculate percentage for the winner
$factor = max($dur1, $dur2) / 100;
$percent = min($dur1, $dur2) / $factor;
$percent = sprintf('%6.2f%% '.FIRST_PLACE, $percent);
// silly edge-case for something like a draw
$draw = false;
if (abs($dur1 - $dur2) < 0.0001) {
$draw = true;
}
// show results
if ($draw) {
echo "Less than 0.0001s difference - seems like a draw!".PHP_EOL;
echo sprintf('%30.s: %.10f s | ' . FIRST_PLACE . PHP_EOL, NAME1, $dur1);
echo sprintf('%30.s: %.10f s | ' . FIRST_PLACE . PHP_EOL, NAME2, $dur2);
} else {
$won = ($dur1 < $dur2) ? 1 : 2;
$winner1 = $won === 1 ? $percent : '100.00% '.SECOND_PLACE;
$winner2 = $won === 2 ? $percent : '100.00% '.SECOND_PLACE;
echo sprintf('%30.s: %.10f s | %s' . PHP_EOL, NAME1, $dur1, $winner1);
echo sprintf('%30.s: %.10f s | %s' . PHP_EOL, NAME2, $dur2, $winner2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment