Skip to content

Instantly share code, notes, and snippets.

@tristanlins
Last active August 29, 2015 14:08
Show Gist options
  • Save tristanlins/2c45ab963aa68ca54e68 to your computer and use it in GitHub Desktop.
Save tristanlins/2c45ab963aa68ca54e68 to your computer and use it in GitHub Desktop.
<?php
$iterations = 1000000;
echo <<<PHP
<?php
\$iterations = $iterations;
\$style = 'default';
PHP;
// -----------------------------------------------------------------------------
// Benchmark direct output
// -----------------------------------------------------------------------------
echo <<<'PHP'
// -----------------------------------------------------------------------------
// Benchmark direct output
// -----------------------------------------------------------------------------
$start = microtime(true);
?>
PHP;
for ($i=0; $i<$iterations; $i++) {
echo <<<PHP
<input class="input$i" />
PHP;
}
echo <<<'PHP'
<?php
$end = microtime(true);
$time1 = $end - $start;
$format = <<<'OUT'
Execution time without array access: %f sec
Memory peak usage: %d bytes
OUT;
$output = sprintf($format, $time1, memory_get_peak_usage());
fwrite(STDERR, $output);
PHP;
// -----------------------------------------------------------------------------
// Benchmark array access output
// -----------------------------------------------------------------------------
echo <<<'PHP'
// -----------------------------------------------------------------------------
// Benchmark array access output
// -----------------------------------------------------------------------------
$GLOBALS['TL_CSS_SETS'] = [
'default' => [
PHP;
for ($i=0; $i<$iterations; $i++) {
echo <<<PHP
'.input$i' => 'input$i',
PHP;
}
echo <<<'PHP'
]
];
$start = microtime(true);
?>
PHP;
for ($i=0; $i<$iterations; $i++) {
echo <<<PHP
<input class="<?= \$GLOBALS['TL_CSS_SETS'][\$style]['.input$i'] ?>" />
PHP;
}
echo <<<'PHP'
<?php
$end = microtime(true);
$time2 = $end - $start;
$format = <<<'OUT'
Execution time with array access: %f sec
Memory peak usage: %d bytes
OUT;
$output = sprintf($format, $time2, memory_get_peak_usage());
fwrite(STDERR, $output);
PHP;
// -----------------------------------------------------------------------------
// Benchmark convenience method access output
// -----------------------------------------------------------------------------
echo <<<'PHP'
// -----------------------------------------------------------------------------
// Benchmark convenience method access output
// -----------------------------------------------------------------------------
class CssClass
{
public static function get($class, $style = null)
{
if (!$style) {
$style = $GLOBALS['style'];
}
return $GLOBALS['TL_CSS_SETS'][$style][$class];
}
}
$start = microtime(true);
?>
PHP;
for ($i=0; $i<$iterations; $i++) {
echo <<<PHP
<input class="<?= CssClass::get('.input$i') ?>" />
PHP;
}
echo <<<'PHP'
<?php
$end = microtime(true);
$time3 = $end - $start;
$format = <<<'OUT'
Execution time with convenience method: %f sec
Memory peak usage: %d bytes
OUT;
$output = sprintf($format, $time3, memory_get_peak_usage());
fwrite(STDERR, $output);
PHP;
echo <<<'PHP'
// -----------------------------------------------------------------------------
// Calculate impact and output results
// -----------------------------------------------------------------------------
$impact1 = $time2 - $time1;
$impact2 = $time3 - $time2;
$format = <<<'OUT'
Iterations / Classes: %d
*** Array access ***
Total impact: %f sec
Impact per class: %f sec
*** Convenience method ***
Total impact: %f sec
Impact per class: %f sec
OUT;
$output = sprintf($format, $iterations, $impact1, $impact1 / $iterations, $impact2, $impact2 / $iterations);
fwrite(STDERR, $output);
PHP;
/* ***************************************************
*
* !!! Shortened example !!!
*
* The generated benchmark.php is about 190MB in size!
*
* ***************************************************
<?php
$iterations = 10;
$style = 'default';
// -----------------------------------------------------------------------------
// Benchmark direct output
// -----------------------------------------------------------------------------
$start = microtime(true);
?>
<input class="input0" />
<input class="input1" />
<input class="input2" />
<input class="input3" />
<input class="input4" />
<input class="input5" />
<input class="input6" />
<input class="input7" />
<input class="input8" />
<input class="input9" />
...
<?php
$end = microtime(true);
$time1 = $end - $start;
$format = <<<'OUT'
Execution time without array access: %f sec
Memory peak usage: %d bytes
OUT;
$output = sprintf($format, $time1, memory_get_peak_usage());
fwrite(STDERR, $output);
// -----------------------------------------------------------------------------
// Benchmark array access output
// -----------------------------------------------------------------------------
$GLOBALS['TL_CSS_SETS'] = [
'default' => [ '.input0' => 'input0',
'.input1' => 'input1',
'.input2' => 'input2',
'.input3' => 'input3',
'.input4' => 'input4',
'.input5' => 'input5',
'.input6' => 'input6',
'.input7' => 'input7',
'.input8' => 'input8',
'.input9' => 'input9',
...
]
];
$start = microtime(true);
?>
<input class="<?= $GLOBALS['TL_CSS_SETS'][$style]['.input0'] ?>" />
<input class="<?= $GLOBALS['TL_CSS_SETS'][$style]['.input1'] ?>" />
<input class="<?= $GLOBALS['TL_CSS_SETS'][$style]['.input2'] ?>" />
<input class="<?= $GLOBALS['TL_CSS_SETS'][$style]['.input3'] ?>" />
<input class="<?= $GLOBALS['TL_CSS_SETS'][$style]['.input4'] ?>" />
<input class="<?= $GLOBALS['TL_CSS_SETS'][$style]['.input5'] ?>" />
<input class="<?= $GLOBALS['TL_CSS_SETS'][$style]['.input6'] ?>" />
<input class="<?= $GLOBALS['TL_CSS_SETS'][$style]['.input7'] ?>" />
<input class="<?= $GLOBALS['TL_CSS_SETS'][$style]['.input8'] ?>" />
<input class="<?= $GLOBALS['TL_CSS_SETS'][$style]['.input9'] ?>" />
...
<?php
$end = microtime(true);
$time2 = $end - $start;
$format = <<<'OUT'
Execution time with array access: %f sec
Memory peak usage: %d bytes
OUT;
$output = sprintf($format, $time2, memory_get_peak_usage());
fwrite(STDERR, $output);
// -----------------------------------------------------------------------------
// Benchmark convenience method access output
// -----------------------------------------------------------------------------
class CssClass
{
public static function get($class, $style = null)
{
if (!$style) {
$style = $GLOBALS['style'];
}
return $GLOBALS['TL_CSS_SETS'][$style][$class];
}
}
$start = microtime(true);
?>
<input class="<?= CssClass::get('.input0') ?>" />
<input class="<?= CssClass::get('.input1') ?>" />
<input class="<?= CssClass::get('.input2') ?>" />
<input class="<?= CssClass::get('.input3') ?>" />
<input class="<?= CssClass::get('.input4') ?>" />
<input class="<?= CssClass::get('.input5') ?>" />
<input class="<?= CssClass::get('.input6') ?>" />
<input class="<?= CssClass::get('.input7') ?>" />
<input class="<?= CssClass::get('.input8') ?>" />
<input class="<?= CssClass::get('.input9') ?>" />
...
<?php
$end = microtime(true);
$time3 = $end - $start;
$format = <<<'OUT'
Execution time with convenience method: %f sec
Memory peak usage: %d bytes
OUT;
$output = sprintf($format, $time3, memory_get_peak_usage());
fwrite(STDERR, $output);
// -----------------------------------------------------------------------------
// Calculate impact and output results
// -----------------------------------------------------------------------------
$impact1 = $time2 - $time1;
$impact2 = $time3 - $time2;
$format = <<<'OUT'
Iterations / Classes: %d
*** Array access ***
Total impact: %f sec
Impact per class: %f sec
*** Convenience method ***
Total impact: %f sec
Impact per class: %f sec
OUT;
$output = sprintf($format, $iterations, $impact1, $impact1 / $iterations, $impact2, $impact2 / $iterations);
fwrite(STDERR, $output);
$ php generate.php > benchmark.php
$ php benchmark.php > /dev/null
Execution time without array access: 0.000018 sec
Memory peak usage: 4002872112 bytes
Execution time with array access: 0.726445 sec
Memory peak usage: 4002872112 bytes
Execution time with convenience method: 1.946270 sec
Memory peak usage: 4002872112 bytes
Iterations / Classes: 1000000
*** Array access ***
Total impact: 0.726427 sec
Impact per class: 0.000001 sec
*** Convenience method ***
Total impact: 1.219825 sec
Impact per class: 0.000001 sec
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment