Skip to content

Instantly share code, notes, and snippets.

@vanchelo
Forked from Big-Shark/speedtest.php
Last active August 29, 2015 14:11
Show Gist options
  • Save vanchelo/4e6476b52fa6fc665b0b to your computer and use it in GitHub Desktop.
Save vanchelo/4e6476b52fa6fc665b0b to your computer and use it in GitHub Desktop.
<?php
<<<CONFIG
packages:
- "lavoiesl/php-benchmark: dev-master"
CONFIG;
/**
* run command "melody run https://gist.githubusercontent.com/vanchelo/4e6476b52fa6fc665b0b/raw/f965054e77d7c431fdfb25ad096e378a3fb1a52f/speedtest.php -vvv"
*/
use Lavoiesl\PhpBenchmark\Benchmark;
class base {
protected $attributes = ['key1' => 'value', 'key2' => 'value2', 'key3' => 'value3', 'key4' => 'value4'];
function _get($key)
{
if (array_key_exists($key, $this->attributes))
{
return $this->attributes[$key];
}
throw new \Exception('');
}
}
class magicGet extends base{
public function __get($key)
{
if(method_exists($this, 'get'.lcfirst($key)))
{
return $this->{'get'.lcfirst($key)}();
}
return $this->_get($key);
}
function getKey1()
{
return $this->_get('key1');
}
}
class magicCall extends base{
public function __call($method, $parameters)
{
$command = substr($method, 0, 3);
$key = lcfirst(substr($method, 3));
if ($command == "set") {
//$this->set($field, $args);
} elseif ($command == "get") {
return $this->_get($key);
}
throw new \Exception("");
}
function getKey1()
{
return $this->_get('key1');
}
}
class noMagic extends base {
function getKey1()
{
return $this->_get('key1');
}
function getKey2()
{
return $this->_get('key2');
}
function getKey3()
{
return $this->_get('key3');
}
function getKey4()
{
return $this->_get('key4');
}
}
declare(ticks=1);
$benchmark = new Benchmark;
$benchmark->setCount(100000);
$magicGet = new magicGet;
$benchmark->add('magicGet', function() use($magicGet) {
$magicGet->key1;
$magicGet->key2;
$magicGet->key3;
$magicGet->key4;
});
$magicCall = new magicCall;
$benchmark->add('magicCall', function() use($magicCall) {
$magicCall->getKey1();
$magicCall->getKey2();
$magicCall->getKey3();
$magicCall->getKey4();
});
$noMagic = new noMagic;
$benchmark->add('noMagic', function() use($noMagic) {
$noMagic->getKey1();
$noMagic->getKey2();
$noMagic->getKey3();
$noMagic->getKey4();
});
$benchmark->run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment