All pastes #1088463 Raw Edit

Miscellany

public php v1 · immutable
#1088463 ·published 2008-07-31 14:33 UTC
rendered paste body
<?phpclass Foo{    private $properties;    public function __construct() {        $this->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 '<html>';echo '<h1>Object Property Benchmarking</h1>';echo '<p>Benchmark of the three most common methods of defining a class and it\'s properties.</p>';$times = array('Foo' => 0, 'Bar' => 0, 'Zip' => 0);echo '<h2>Instanciation</h2>';echo '<p>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 '<b>' . $times['Foo'] . 'ms</b> (' . $t2 . '&micro;s - ' . $t1 . '&micro;s)</p>';echo '<p>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 '<b>' . $times['Bar'] . 'ms</b> (' . $t2 . '&micro;s - ' . $t1 . '&micro;s)</p>';echo '<p>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 '<b>' . $times['Zip'] . 'ms</b> (' . $t2 . '&micro;s - ' . $t1 . '&micro;s)</p>';asort($times);$objs = array_keys($times);echo '<p><b>' . $objs[0] . '</b> is the most efficient.</p>';echo '<H2>Property Handling</h2>';$x = 0;echo '<p>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 '<b>' . $times['Foo'] . 'ms</b> (' . $t2 . '&micro;s - ' . $t1 . '&micro;s)</p>';echo '<p>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 '<b>' . $times['Bar'] . 'ms</b> (' . $t2 . '&micro;s - ' . $t1 . '&micro;s)</p>';echo '<p>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 '<b>' . $times['Zip'] . 'ms</b> (' . $t2 . '&micro;s - ' . $t1 . '&micro;s)</p>';asort($times);$objs = array_keys($times);echo '<p><b>' . $objs[0] . '</b> is the most efficient.</p>';echo '<h2>Class Definitions</h2>';$f = file(__FILE__);$source = join(array_slice($f, 1, 69));echo '<pre>';highlight_string($source);echo '</pre>';echo '</html>';?>