Skip to content

Instantly share code, notes, and snippets.

@pronskiy
Last active November 14, 2020 18:10
Show Gist options
  • Save pronskiy/492a0590d0fd3cfa0a02a2d7f908c494 to your computer and use it in GitHub Desktop.
Save pronskiy/492a0590d0fd3cfa0a02a2d7f908c494 to your computer and use it in GitHub Desktop.
Benchmarks of accessing properties
<?php
class Test {
// Enforce type using typed property.
public string $publicTypedProp = "default";
// Enforce type by going through getter and setter.
private $privateProp = "default";
public function getPrivateProp(): string {
return $this->privateProp;
}
public function setPrivateProp(string $value): void {
$this->privateProp = $value;
}
public function setPrivatePropNoType($value) {
$this->privateProp = $value;
}
// Enforce type by going through getter and setter,
// but make it look like a normal property access using magic.
public function __get(string $name) {
return $this->{'get'.$name}();
}
public function __set(string $name, $value): void {
$this->{'set'.$name}($value);
}
}
$n = 100000000;
$obj = new Test;
$startTime = microtime(true);
for ($i = 0; $i < $n; $i++) {
$obj->publicTypedProp = "new";
}
echo "Set typed public property: ", microtime(true) - $startTime, "s\n";
$startTime = microtime(true);
for ($i = 0; $i < $n; $i++) {
$obj->setPrivatePropNoType("new");
}
echo "Set via setter (no type check): ", microtime(true) - $startTime, "s\n";
$startTime = microtime(true);
for ($i = 0; $i < $n; $i++) {
$obj->setPrivateProp("new");
}
echo "Set private property via setter: ", microtime(true) - $startTime, "s\n";
$startTime = microtime(true);
for ($i = 0; $i < $n; $i++) {
$obj->privateProp = "new";
}
echo "Set private property via magic: ", microtime(true) - $startTime, "s\n";
$startTime = microtime(true);
for ($i = 0; $i < $n; $i++) {
$value = $obj->publicTypedProp;
}
echo "Get typed public property: ", microtime(true) - $startTime, "s\n";
$startTime = microtime(true);
for ($i = 0; $i < $n; $i++) {
$value = $obj->getPrivateProp();
}
echo "Get private property via getter: ", microtime(true) - $startTime, "s\n";
$startTime = microtime(true);
for ($i = 0; $i < $n; $i++) {
$value = $obj->privateProp;
}
echo "Get private property via magic: ", microtime(true) - $startTime, "s\n";
@pronskiy
Copy link
Author

Set typed public property:       1.408833026886s
Set via setter (no type check):  3.0818917751312s
Set private property via setter: 3.1216759681702s
Set private property via magic:  20.88130402565s
Get typed public property:       1.2220709323883s
Get private property via getter: 3.2079019546509s
Get private property via magic:  17.236463069916s

@zloyuser
Copy link

zloyuser commented Dec 20, 2019

Для полноты картины:

<?php
class Test {
    // Enforce type using typed property.
    public string $publicTypedProp = "default";
    public $publicProp = "default";
    // Enforce type by going through getter and setter.
    private $privateProp = "default";
    public function getPrivateProp(): string {
        return $this->privateProp;
    }
    public function setPrivateProp(string $value): void {
        $this->privateProp = $value;
    }
    public function setPrivatePropNoType($value) {
        $this->privateProp = $value;
    }
    // Enforce type by going through getter and setter,
    // but make it look like a normal property access using magic.
    public function __get(string $name) {
        return $this->{'get'.$name}();
    }
    public function __set(string $name, $value): void {
        $this->{'set'.$name}($value);
    }
}

$n = 100000000;
$obj = new Test;

$startTime = microtime(true);
for ($i = 0; $i < $n; $i++) {
    $obj->publicTypedProp = "new";
}
echo "Set typed public property:       ", microtime(true) - $startTime, "s\n";

$startTime = microtime(true);
for ($i = 0; $i < $n; $i++) {
    $obj->publicProp = "new";
}
echo "Set public property:             ", microtime(true) - $startTime, "s\n";

$startTime = microtime(true);
for ($i = 0; $i < $n; $i++) {
    $obj->setPrivatePropNoType("new");
}
echo "Set via setter (no type check):  ", microtime(true) - $startTime, "s\n";

$startTime = microtime(true);
for ($i = 0; $i < $n; $i++) {
    $obj->setPrivateProp("new");
}
echo "Set private property via setter: ", microtime(true) - $startTime, "s\n";

$startTime = microtime(true);
for ($i = 0; $i < $n; $i++) {
    $obj->privateProp = "new";
}
echo "Set private property via magic:  ", microtime(true) - $startTime, "s\n";

$startTime = microtime(true);
for ($i = 0; $i < $n; $i++) {
    $value = $obj->publicTypedProp;
}
echo "Get typed public property:       ", microtime(true) - $startTime, "s\n";

$startTime = microtime(true);
for ($i = 0; $i < $n; $i++) {
    $value = $obj->publicProp;
}
echo "Get public property:             ", microtime(true) - $startTime, "s\n";

$startTime = microtime(true);
for ($i = 0; $i < $n; $i++) {
    $value = $obj->getPrivateProp();
}
echo "Get private property via getter: ", microtime(true) - $startTime, "s\n";

$startTime = microtime(true);
for ($i = 0; $i < $n; $i++) {
    $value = $obj->privateProp;
}
echo "Get private property via magic:  ", microtime(true) - $startTime, "s\n";

И результаты:

Set typed public property:       1.0989079475403s
Set public property:             0.92198896408081s
Set via setter (no type check):  2.5118339061737s
Set private property via setter: 2.4649128913879s
Set private property via magic:  10.380884170532s
Get typed public property:       0.88247489929199s
Get public property:             0.88888597488403s
Get private property via getter: 2.5427129268646s
Get private property via magic:  9.7574009895325s

@pronskiy
Copy link
Author

@zloyuser thanks 👍

@SDKiller
Copy link

Frameworks...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment