Last active
August 29, 2015 14:23
-
-
Save kaja47/dabac8cf0fa0d8784bd2 to your computer and use it in GitHub Desktop.
PHP polymorphic inline caches
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class A { | |
public $somePropertyWithReasonablyLongName = 1; | |
} | |
class B { | |
public $somePropertyWithReasonablyLongName = 1; | |
} | |
// AAAA | |
$arr = []; | |
for ($i = 0; $i < 256; $i++) { | |
$arr[$i] = new A; | |
} | |
$s = microtime(1); | |
for ($i = 0; $i < 10000000; $i++) { | |
$arr[$i & 0xff]->somePropertyWithReasonablyLongName; | |
} | |
echo microtime(1) - $s, " s\n"; | |
// ABAB | |
$arr = []; | |
for ($i = 0; $i < 256; $i++) { | |
$arr[$i] = (($i % 2 === 0) ? new A : new B); | |
} | |
$s = microtime(1); | |
for ($i = 0; $i < 10000000; $i++) { | |
$arr[$i & 0xff]->somePropertyWithReasonablyLongName; | |
} | |
echo microtime(1) - $s, " s\n"; | |
// PHP 5.6 PHP 7.0 | |
// ------------------------- | |
// AAAA 0.6 s 0.3 s | |
// ABAB 0.7 s 0.4 s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment