Skip to content

Instantly share code, notes, and snippets.

@homeyjd
Forked from ranacseruet/gist:8412771
Created January 7, 2015 04:55
Show Gist options
  • Save homeyjd/c1e185fc35832e628f5d to your computer and use it in GitHub Desktop.
Save homeyjd/c1e185fc35832e628f5d to your computer and use it in GitHub Desktop.
Benchmark ArrayAccess vs. Magic Methods __get/__set() in PHP classes
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
class Test implements ArrayAccess
{
public $data = array();
public function __set($key, $value){
$this->data[$key] = $value;
}
public function set($key, $value){
$this->__set($key, $value);
}
public function __get($key){
return $this->data[$key];
}
public function get($key){
return $this->__get($key);
}
public function offsetExists($offset) {
}
public function offsetGet($offset) {
return $this->__get($offset);
}
public function offsetSet($offset, $value) {
$this->__set($offset, $value);
}
public function offsetUnset($offset) {
}
}
if (php_sapi_name() !== 'cli') {
echo "<pre>\n";
}
$iterations = pow(10,6);
echo "Running $iterations iterations...\n";
echo 'Working...';
$time = microtime(true);
for ($i=0; $i < $iterations; $i++) {
$test = new Test;
$test["a"] = "test a";
$test["b"] = "test b";
$test["c"] = "test c";
$temp = $test["a"];
$temp = $test["b"];
$temp = $test["c"];
}
printf("\r%7.0f/s Test[key]=value (array access)\n", $iterations/(microtime(true)-$time));
echo 'Working...';
$time = microtime(true);
for ($i=0; $i < $iterations; $i++) {
$test = new Test;
$test->__set("a", "test a");
$test->__set("b", "test b");
$test->__set("c", "test c");
$temp = $test->__get("a");
$temp = $test->__get("b");
$temp = $test->__get("c");
}
printf("\r%7.0f/s Test->__set(key,value) (function call)\n", $iterations/(microtime(true)-$time));
echo 'Working...';
$time = microtime(true);
for ($i=0; $i < $iterations; $i++) {
$test = new Test;
$test->set("a", "test a");
$test->set("b", "test b");
$test->set("c", "test c");
$temp = $test->get("a");
$temp = $test->get("b");
$temp = $test->get("c");
}
printf("\r%7.0f/s Test->set(key,value) (function call)\n", $iterations/(microtime(true)-$time));
echo 'Working...';
$time = microtime(true);
for ($i=0; $i < $iterations; $i++) {
$test = new Test;
$test->a = "test a";
$test->b = "test b";
$test->c = "test c";
$temp = $test->a;
$temp = $test->b;
$temp = $test->c;
}
printf("\r%7.0f/s Test->key=value (Object property)\n", $iterations/(microtime(true)-$time));
echo 'Working...';
$time = microtime(true);
for ($i=0; $i < $iterations; $i++) {
$test = new stdClass;
$test->a = "test a";
$test->b = "test b";
$test->c = "test c";
$temp = $test->a;
$temp = $test->b;
$temp = $test->c;
}
printf("\r%7.0f/s stdClass->key=value (original)\n", $iterations/(microtime(true)-$time));
echo 'Working...';
$time = microtime(true);
for ($i=0; $i < $iterations; $i++) {
$test = array();
$test["a"] = "test a";
$test["b"] = "test b";
$test["c"] = "test c";
$temp = $test["a"];
$temp = $test["b"];
$temp = $test["c"];
}
printf("\r%7.0f/s Array[key]=value (original)\n", $iterations/(microtime(true)-$time));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment