normal(); } $stop = microtime(true); echo "Native Method: " . ($stop - $start) . " seconds". PHP_EOL; $start = microtime(true); for ($i=0; $i < ITERATIONS; ++$i) { $t->doesntExist(); } $stop = microtime(true); echo "Magic Method: " . ($stop - $start) . " seconds". PHP_EOL; /////////////////////////////////////////////////////////////////// echo "Testing __call() with sub-function". PHP_EOL; class TestCallSub { function normal() { return; } function bar() { return; } function __call($method, $args) { if ($method == 'foo') { return call_user_func_array(array($this, 'bar'), $args); } return; } } $t = new TestCallSub(); $start = microtime(true); for ($i=0; $i < ITERATIONS; ++$i) { $t->normal(); } $stop = microtime(true); echo "Native Method: " . ($stop - $start) . " seconds". PHP_EOL; $start = microtime(true); for ($i=0; $i < ITERATIONS; ++$i) { $t->foo(); } $stop = microtime(true); echo "Magic Method: " . ($stop - $start) . " seconds". PHP_EOL; /////////////////////////////////////////////////////////////////// echo "Testing __get() and __set()". PHP_EOL; class TestGetSet { public $foo = 1; public function __get($var) { return $this->foo; } public function __set($var, $val) { $this->foo = $val; } } $t = new TestGetSet(); $start = microtime(true); for ($i=0; $i < ITERATIONS; ++$i) { $t->foo; } $stop = microtime(true); echo "Get Native Property: " . ($stop - $start) . " seconds". PHP_EOL; $start = microtime(true); for ($i=0; $i < ITERATIONS; ++$i) { $t->bar; } $stop = microtime(true); echo "Get Magic Property: " . ($stop - $start) . " seconds". PHP_EOL; $start = microtime(true); for ($i=0; $i < ITERATIONS; ++$i) { $t->foo = 1; } $stop = microtime(true); echo "Set Native Property: " . ($stop - $start) . " seconds". PHP_EOL; $start = microtime(true); for ($i=0; $i < ITERATIONS; ++$i) { $t->bar = 1; } $stop = microtime(true); echo "Set Magic Property: " . ($stop - $start) . " seconds". PHP_EOL; /////////////////////////////////////////////////////////////////// echo "Testing ArrayAccess". PHP_EOL; class TestArrayAccess implements ArrayAccess { private $properties = array(); public $foo = 1; function __construct($array) { $this->properties = $array; } function offsetExists($offset) { return isset($this->properties[$offset]); } function offsetGet($offset) { return $this->properties[$offset]; } function offsetSet($offset, $value) { $this->properties[$offset] = $value; } function offsetUnset($offset) { unset($this->properties[$offset]); } } $a = array('a' => 'A', 'b' => 'B', 'c' => 'C', 'd' => 'D'); $t = new TestArrayAccess($a); $start = microtime(true); for ($i=0; $i < ITERATIONS; ++$i) { $a['b']; } $stop = microtime(true); echo "Get Array Property: " . ($stop - $start) . " seconds". PHP_EOL; $start = microtime(true); for ($i=0; $i < ITERATIONS; ++$i) { $a['b'] = 'B'; } $stop = microtime(true); echo "Set Array Property: " . ($stop - $start) . " seconds". PHP_EOL; $start = microtime(true); for ($i=0; $i < ITERATIONS; ++$i) { $t->foo; } $stop = microtime(true); echo "Get Object Property: " . ($stop - $start) . " seconds". PHP_EOL; $start = microtime(true); for ($i=0; $i < ITERATIONS; ++$i) { $t->foo = 1; } $stop = microtime(true); echo "Set Object Property: " . ($stop - $start) . " seconds". PHP_EOL; $start = microtime(true); for ($i=0; $i < ITERATIONS; ++$i) { $t['b']; } $stop = microtime(true); echo "Get ArrayAccess Property: " . ($stop - $start) . " seconds". PHP_EOL; $start = microtime(true); for ($i=0; $i < ITERATIONS; ++$i) { $t['b'] = 'B'; } $stop = microtime(true); echo "Set ArrayAccess Property: " . ($stop - $start) . " seconds". PHP_EOL; /////////////////////////////////////////////////////////////////// echo "Testing Inheritance". PHP_EOL; class Base { public $test = 1; public function baseMethod() { return; } public function overrideMe() { return; } } class Child extends Base { public $child = 1; public function childMethod() { return; } public function overrideMe() { return; } } $b = new Base(); $c = new Child(); $start = microtime(true); for ($i=0; $i < ITERATIONS; ++$i) { $b->test; } $stop = microtime(true); echo "Get Base Property: " . ($stop - $start) . " seconds". PHP_EOL; $start = microtime(true); for ($i=0; $i < ITERATIONS; ++$i) { $c->test; } $stop = microtime(true); echo "Get Base Property from Child: " . ($stop - $start) . " seconds". PHP_EOL; $start = microtime(true); for ($i=0; $i < ITERATIONS; ++$i) { $c->child; } $stop = microtime(true); echo "Get Child Property: " . ($stop - $start) . " seconds". PHP_EOL; $start = microtime(true); for ($i=0; $i < ITERATIONS; ++$i) { $b->baseMethod(); } $stop = microtime(true); echo "Get Base Method: " . ($stop - $start) . " seconds". PHP_EOL; $start = microtime(true); for ($i=0; $i < ITERATIONS; ++$i) { $c->baseMethod(); } $stop = microtime(true); echo "Get Base Method from Child: " . ($stop - $start) . " seconds". PHP_EOL; $start = microtime(true); for ($i=0; $i < ITERATIONS; ++$i) { $c->childMethod(); } $stop = microtime(true); echo "Get Child Method: " . ($stop - $start) . " seconds". PHP_EOL; $start = microtime(true); for ($i=0; $i < ITERATIONS; ++$i) { $c->overrideMe(); } $stop = microtime(true); echo "Override Child Method: " . ($stop - $start) . " seconds". PHP_EOL; /////////////////////////////////////////////////////////////////// echo "Testing Composition". PHP_EOL; class Used { public function myMethod() { return; } } class User { protected $used; public function __construct() { $this->used = new Used(); } public function myMethod() { return $this->used->myMethod(); } } $u = new User(); $start = microtime(true); for ($i=0; $i < ITERATIONS; ++$i) { $u->myMethod(); } $stop = microtime(true); echo "Get Composed Method: " . ($stop - $start) . " seconds". PHP_EOL; /////////////////////////////////////////////////////////////////// echo "Testing Iteration". PHP_EOL; class Internal implements Iterator { protected $a; public function __construct(array $a) { $this->a = $a; } public function current() { return current($this->a); } public function key() { return key($this->a); } public function next() { return next($this->a); } public function rewind() { return reset($this->a); } public function valid() { return (current($this->a) !== FALSE); } } class External implements IteratorAggregate { protected $a; public function __construct(array $a) { $this->a = $a; } public function getIterator() { return new ArrayIterator($this->a); } } $a = array('A', 'B', 'C', 'D'); $internal = new Internal($a); $external = new External($a); $start = microtime(true); for ($i=0; $i < ITERATIONS; ++$i) { foreach ($a as $item); } $stop = microtime(true); echo "Iterate array: " . ($stop - $start) . " seconds". PHP_EOL; $start = microtime(true); for ($i=0; $i < ITERATIONS; ++$i) { foreach ($internal as $item); } $stop = microtime(true); echo "Iterate internal iterator: " . ($stop - $start) . " seconds". PHP_EOL; $start = microtime(true); for ($i=0; $i < ITERATIONS; ++$i) { foreach ($external as $item); } $stop = microtime(true); echo "Iterate external iterator: " . ($stop - $start) . " seconds". PHP_EOL;