Skip to content

Instantly share code, notes, and snippets.

@nikic
Created May 3, 2021 12:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nikic/193c42cd5e8290d5aacb93b7125d5b4f to your computer and use it in GitHub Desktop.
Save nikic/193c42cd5e8290d5aacb93b7125d5b4f to your computer and use it in GitHub Desktop.
<?php
class Test {
public $prop1 = null;
public $prop2 = null { get; set; }
private $_prop3 = null;
public $prop3 {
get { return $this->_prop3; }
set { $this->_prop3 = $value; }
}
private $_prop4 = null;
public function __get($name) {
if ($name === 'prop4') {
return $this->_prop4;
}
throw new Error("Unknown property $name");
}
public function __set($name, $value) {
if ($name === 'prop4') {
$this->_prop4 = $value;
return;
}
throw new Error("Unknown property $name");
}
public function __isset($name) {
return $name === 'prop4';
}
private $_prop5 = null;
public function getProp5() {
return $this->_prop5;
}
public function setProp5($value) {
$this->_prop5 = $value;
}
}
$test = new Test;
$n = 10000000;
$m = 0b11111;
if ($m & 0b1) {
$t = microtime(true);
for ($i = 0; $i < $n; $i++) {
$test->prop1;
}
printf("Normal property read: %fs\n", microtime(true) - $t);
$t = microtime(true);
for ($i = 0; $i < $n; $i++) {
$test->prop1 = null;
}
printf("Normal property write: %fs\n", microtime(true) - $t);
$t = microtime(true);
for ($i = 0; $i < $n; $i++) {
isset($test->prop1);
}
printf("Normal property isset: %fs\n", microtime(true) - $t);
}
if ($m & 0b10) {
$t = microtime(true);
for ($i = 0; $i < $n; $i++) {
$test->prop2;
}
printf("Auto accessor property read: %fs\n", microtime(true) - $t);
$t = microtime(true);
for ($i = 0; $i < $n; $i++) {
$test->prop2 = null;
}
printf("Auto accessor property write: %fs\n", microtime(true) - $t);
$t = microtime(true);
for ($i = 0; $i < $n; $i++) {
isset($test->prop2);
}
printf("Auto accessor property isset: %fs\n", microtime(true) - $t);
}
if ($m & 0b100) {
$t = microtime(true);
for ($i = 0; $i < $n; $i++) {
$test->prop3;
}
printf("Accessor property read: %fs\n", microtime(true) - $t);
$t = microtime(true);
for ($i = 0; $i < $n; $i++) {
$test->prop3 = null;
}
printf("Accessor property write: %fs\n", microtime(true) - $t);
$t = microtime(true);
for ($i = 0; $i < $n; $i++) {
isset($test->prop3);
}
printf("Accessor property isset: %fs\n", microtime(true) - $t);
}
if ($m & 0b1000) {
$t = microtime(true);
for ($i = 0; $i < $n; $i++) {
$test->prop4;
}
printf("Magic property read: %fs\n", microtime(true) - $t);
$t = microtime(true);
for ($i = 0; $i < $n; $i++) {
$test->prop4 = null;
}
printf("Magic property write: %fs\n", microtime(true) - $t);
$t = microtime(true);
for ($i = 0; $i < $n; $i++) {
isset($test->prop4);
}
printf("Magic property isset: %fs\n", microtime(true) - $t);
}
if ($m & 0b10000) {
$t = microtime(true);
for ($i = 0; $i < $n; $i++) {
$test->getProp5();
}
printf("Method property read: %fs\n", microtime(true) - $t);
$t = microtime(true);
for ($i = 0; $i < $n; $i++) {
$test->setProp5(null);
}
printf("Method property write: %fs\n", microtime(true) - $t);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment