Members, __set, __get, ArrayAccess and Iterator
Lessons learned:
Here is the code:
- __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:
Tested with PHP 5.4.5
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) {
return $this->_data[$id];
}
}
$start = microtime(true);
$c = new test2();
for ($i=0; $i<1000000; $i++) $c->test3++;
echo number_format(microtime(true)-$start, 4)."\n"; // 0.1495s
$start = microtime(true);
$c = new test2();
for ($i=0; $i<1000000; $i++) $c->test4++;
echo number_format(microtime(true)-$start, 4)."\n"; // 2.0083s
class test3 implements ArrayAccess {
private $_data = [0];
public $data = [0];
public function offsetSet($key, $val) {
$this->_data[$key] = $val;
}
public function offsetGet($key) {
return $this->_data[$key];
}
public function offsetExists($key) {
return isset($this->_data[$key]);
}
public function offsetUnset($key) {
unset($this->_data[$key]);
}
}
$start = microtime(true);
$c = new test3();
for ($i=0; $i<1000000; $i++) $c->data[0] = $i;
echo number_format(microtime(true)-$start, 4)."\n"; // 0.2739s
$start = microtime(true);
$c = new test3();
for ($i=0; $i<1000000; $i++) $c[0] = $i;
echo number_format(microtime(true)-$start, 4)."\n"; // 0.9724s
$start = microtime(true);
$c = new test3();
for ($i=0; $i<1000000; $i++) $c->data[0]++;
echo number_format(microtime(true)-$start, 4)."\n"; // 0.2694s
$start = microtime(true);
$c = new test3();
for ($i=0; $i<1000000; $i++) $c[0] = $c[0]+1; // $c[0]++ does not work here
echo number_format(microtime(true)-$start, 4)."\n"; // 1.6771s
$start = microtime(true);
$c = new test3();
for ($i=0; $i<1000000; $i++) $c->data[] = $i;
echo number_format(microtime(true)-$start, 4)."\n"; // 0.4906s
$start = microtime(true);
$c = new test3();
for ($i=0; $i<1000000; $i++) $c[$i] = $i; // $c[] does not work here
echo number_format(microtime(true)-$start, 4)."\n"; // 1.3456s
class test4 {
public $data = [];
public function __construct() {
$this->data = range(0,500000);
}
}
$start = microtime(true);
$c = new test4();
foreach ($c->data as $val) {}
echo number_format(microtime(true)-$start, 4)."\n"; // 0.2541s
$start = microtime(true);
$c = new test4();
foreach ($c->data as $key=>$val) {}
echo number_format(microtime(true)-$start, 4)."\n"; // 0.2704s
class test5 implements Iterator {
private $_pos = 0;
private $_data = [];
public function __construct() {
$this->_data = range(0,500000);
}
function rewind() {
$this->_pos = 0;
}
function current() {
return $this->_data[$this->_pos];
}
function key() {
return $this->_pos;
}
function next() {
$this->_pos++;
}
function valid() {
return isset($this->_data[$this->_pos]);
}
}
$start = microtime(true);
$c = new test5();
foreach ($c as $val) {}
echo number_format(microtime(true)-$start, 4)."\n"; // 0.9770s
$start = microtime(true);
$c = new test5();
foreach ($c as $key=>$val) {}
echo number_format(microtime(true)-$start, 4)."\n"; // 1.0400s
Comments
Post a Comment