Skip to content

Instantly share code, notes, and snippets.

@kaja47
Last active August 29, 2015 14:23
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 kaja47/dabac8cf0fa0d8784bd2 to your computer and use it in GitHub Desktop.
Save kaja47/dabac8cf0fa0d8784bd2 to your computer and use it in GitHub Desktop.
PHP polymorphic inline caches
<?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