This file contains 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 | |
class dog { | |
public $name = ''; | |
public function setName($name) { | |
$this->name = $name; | |
} | |
public function getName() { | |
return $this->name; | |
} | |
} | |
class cat { | |
protected $name = ''; | |
public function __set($name, $value) { | |
$this->$name = $value; | |
} | |
public function __get($name) { | |
return $this->$name; | |
} | |
} | |
class worm implements ArrayAccess { | |
public $name = ''; | |
public function offsetUnset($name) {} | |
public function offsetExists($name) {} | |
public function offsetSet($name, $value) { | |
$this->$name = $value; | |
} | |
public function offsetGet($name) { | |
return $this->$name; | |
} | |
} | |
function test_getters_setters() { | |
echo __FUNCTION__ . ":\t" ; | |
$Class = new dog(); | |
$s = 0; | |
for ($x=0; $x<10; $x++) { | |
$t = microtime(true); | |
for ($i=0; $i<1000000; $i++) { | |
$Class->setName("rover"); | |
$n = $Class->getName(); | |
} | |
$s += microtime(true) - $t; | |
} | |
echo ($s / $x) . "\n"; | |
} | |
function test_public_properties() { | |
echo __FUNCTION__ . ":\t" ; | |
$Class = new dog(); | |
$s = 0; | |
for ($x=0; $x<10; $x++) { | |
$t = microtime(true); | |
for ($i=0; $i<1000000; $i++) { | |
$Class->name = "Rover"; | |
$n = $Class->name; | |
} | |
$s += microtime(true) - $t; | |
} | |
echo ($s / $x) . "\n"; | |
} | |
function test_magic_methods() { | |
echo __FUNCTION__ . ":\t" ; | |
$Class = new cat(); | |
$s = 0; | |
for ($x=0; $x<10; $x++) { | |
$t = microtime(true); | |
for ($i=0; $i<1000000; $i++) { | |
$Class->name = "Rover"; | |
$n = $Class->name; | |
} | |
$s += microtime(true) - $t; | |
} | |
echo ($s / $x) . "\n"; | |
} | |
function test_array_access() { | |
echo __FUNCTION__ . ":\t" ; | |
$Class = new worm(); | |
$s = 0; | |
for ($x=0; $x<10; $x++) { | |
$t = microtime(true); | |
for ($i=0; $i<1000000; $i++) { | |
$Class['name'] = "Rover"; | |
$n = $Class['name']; | |
} | |
$s += microtime(true) - $t; | |
} | |
echo ($s / $x) . "\n"; | |
} | |
test_getters_setters(); | |
test_public_properties(); | |
test_magic_methods(); | |
test_array_access(); |
Output on my Ubuntu VM (PHP 5.3.5):
test_getters_setters: 0.64945447444916 test_public_properties: 0.27986690998077 test_magic_methods: 1.0444820642471 test_array_access: 0.9640593290329
WTF? How many getters/setters You use in your project per request?
Source: Let's make the web faster by Google