Posts

Showing posts with the label OOP

Members, __set, __get, ArrayAccess and Iterator

Lessons learned: __set() is 5 times slower than setting a member __get() + __set() is 13 times slower than incrementing a member Iterator is 4 times slower than using a member ArrayAccess is 3 times slower than setting an element in a member array ArrayAccess is 6 times slower than incrementing an element in a member array Here is the code: class test1 { public $test = null; private $_test = null; public function __set($id, $val) { $this->_test = $val; } } $start = microtime(true); $c = new test1(); for ($i=0; $i<1000000; $i++) $c->test = $i; echo number_format(microtime(true)-$start, 4)."\n"; // 0.1830s $start = microtime(true); $c = new test1(); for ($i=0; $i<1000000; $i++) $c->test2 = $i; echo number_format(microtime(true)-$start, 4)."\n"; // 0.9570s class test2 { private $_data = ['test4'=>0]; public $test3 = 0; public function __set($id, $val) { $this->_data[$id] = $val; } public function __get($id) { ...