properties = array('foo' => null, 'bar' => null, 'zip' => null); } public function __get($prop) { return $this->properties[$prop]; } public function __set($prop, $value) { $this->properties[$prop] = $value; } } class Bar { private $foo; private $bar; private $zip; public function __construct() { $this->foo = null; $this->bar = null; $this->zip = null; } public function getFoo() { return $this->foo; } public function setFoo($value) { $this->foo = $value; } public function getBar() { return $this->bar; } public function setBar($value) { $this->bar = $value; } public function getZip() { return $this->zip; } public function setZip($value) { $this->zip = $value; } } class Zip { public $foo; public $bar; public $zip; public function __construct() { $this->foo = null; $this->bar = null; $this->zip = null; } } echo ''; echo '

Object Property Benchmarking

'; echo '

Benchmark of the three most common methods of defining a class and it\'s properties.

'; $times = array('Foo' => 0, 'Bar' => 0, 'Zip' => 0); echo '

Instanciation

'; echo '

Creating Foo 100,000 times: '; flush(); $t1 = microtime(true); for ($i = 0; $i < 100000; $i++) { $foo = new Foo(); unset($foo); } $t2 = microtime(true); $times['Foo'] = round(($t2 - $t1) * 100, 2); echo '' . $times['Foo'] . 'ms (' . $t2 . 'µs - ' . $t1 . 'µs)

'; echo '

Creating Bar 100,000 times: '; flush(); $t1 = microtime(true); for ($i = 0; $i < 100000; $i++) { $bar = new Bar(); unset($bar); } $t2 = microtime(true); $times['Bar'] = round(($t2 - $t1) * 100, 2); echo '' . $times['Bar'] . 'ms (' . $t2 . 'µs - ' . $t1 . 'µs)

'; echo '

Creating Zip 100,000 times: '; flush(); $t1 = microtime(true); for ($i = 0; $i < 100000; $i++) { $zip = new Zip(); unset($zip); } $t2 = microtime(true); $times['Zip'] = round(($t2 - $t1) * 100, 2); echo '' . $times['Zip'] . 'ms (' . $t2 . 'µs - ' . $t1 . 'µs)

'; asort($times); $objs = array_keys($times); echo '

' . $objs[0] . ' is the most efficient.

'; echo '

Property Handling

'; $x = 0; echo '

Manipulating Foo\'s properties 100,000 times: '; flush(); $foo = new Foo(); $t1 = microtime(true); for ($i = 0; $i < 100000; $i++) { $foo->foo = $i; $x = $foo->foo; $foo->bar = $i; $x = $foo->bar; $foo->zip = $i; $x = $foo->zip; } $t2 = microtime(true); unset($foo); $times['Foo'] = round(($t2 - $t1) * 100, 2); echo '' . $times['Foo'] . 'ms (' . $t2 . 'µs - ' . $t1 . 'µs)

'; echo '

Manipulating Bar\'s properties 100,000 times: '; flush(); $bar = new Bar(); $t1 = microtime(true); for ($i = 0; $i < 100000; $i++) { $bar->setFoo($i); $x = $bar->getFoo(); $bar->setBar($i); $x = $bar->getBar(); $bar->setZip($i); $x = $bar->getZip(); } $t2 = microtime(true); unset($bar); $times['Bar'] = round(($t2 - $t1) * 100, 2); echo '' . $times['Bar'] . 'ms (' . $t2 . 'µs - ' . $t1 . 'µs)

'; echo '

Manipulating Zip\'s properties 100,000 times: '; flush(); $zip = new Zip(); $t1 = microtime(true); for ($i = 0; $i < 100000; $i++) { $zip->foo = $i; $x = $zip->foo; $zip->bar = $i; $x = $zip->bar; $zip->zip = $i; $x = $zip->zip; } $t2 = microtime(true); unset($zip); $times['Zip'] = round(($t2 - $t1) * 100, 2); echo '' . $times['Zip'] . 'ms (' . $t2 . 'µs - ' . $t1 . 'µs)

'; asort($times); $objs = array_keys($times); echo '

' . $objs[0] . ' is the most efficient.

'; echo '

Class Definitions

'; $f = file(__FILE__); $source = join(array_slice($f, 1, 69)); echo '
';
highlight_string($source);
echo '
'; echo ''; ?>