Skip to content

Instantly share code, notes, and snippets.

@ohader
Last active March 6, 2017 09:56
Show Gist options
  • Save ohader/5650a4969beaafccf5943e3fefd51c37 to your computer and use it in GitHub Desktop.
Save ohader/5650a4969beaafccf5943e3fefd51c37 to your computer and use it in GitHub Desktop.
PHP tick call analysis
<?php
/*
* Output:
*
* T
* #1:TTT -> 2 ticks (removed echo tick)
* #2:TT -> 1 tick (removed echo tick)
* #3:TTTT -> 3 ticks (removed echo tick)
* #4:TTT -> 2 ticks (removed echo tick)
* end:T% *
*
* One tick has to be removed for each section, which is the echo invocation.
* Thus, only variant #2 is faster by one tick...
*/
class Model {
private $value = 999;
public function getValue()
{
return $this->value;
}
}
$model = new Model();
declare(ticks=1);
register_tick_function(function() { echo 'T'; }, true);
echo PHP_EOL . '#1:';
if ($model->getValue() === -1) {
}
if ($model->getValue() === 0) {
}
echo PHP_EOL . '#2:';
if ($model->getValue() === -1 || $model->getValue() === 0) {
}
echo PHP_EOL . '#3:';
$value = $model->getValue();
if ($value === -1) {
}
if ($value === 0) {
}
echo PHP_EOL . '#4:';
$value = $model->getValue();
if ($value === -1 || $value === 0) {
}
echo PHP_EOL . 'end:';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment