Last active
August 29, 2015 14:11
-
-
Save Big-Shark/b8231dde88e3639add70 to your computer and use it in GitHub Desktop.
speed test magic function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
<<<CONFIG | |
packages: | |
- "lavoiesl/php-benchmark: dev-master" | |
CONFIG; | |
/** | |
* run command "melody run https://gist.github.com/Big-Shark/b8231dde88e3639add70" | |
* | |
*/ | |
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(10000); | |
$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