Last active
June 24, 2019 00:31
-
-
Save muglug/e3b8bb7dbbec4431c71ae541f61476e6 to your computer and use it in GitHub Desktop.
Test for ways of storing node types
This file contains hidden or 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 Tree { | |
public $left = null; | |
public $right = null; | |
} | |
class Type { | |
/** @var int */ | |
public $i = 0; | |
} | |
class Context { | |
private $cache = []; | |
public function getType(int $node_id) : ?Type | |
{ | |
return $this->cache[$node_id] ?? null; | |
} | |
public function setType(int $node_id, Type $type) : void | |
{ | |
$this->cache[$node_id] = $type; | |
} | |
} | |
$tree1 = new Tree(); | |
$tree2 = new Tree(); | |
$tree3 = new Tree(); | |
function branchTree(Tree $tree, int $i) : void { | |
if (--$i < 0) { | |
return; | |
} | |
$tree->left = new Tree; | |
$tree->right = new Tree; | |
branchTree($tree->left, $i); | |
branchTree($tree->right, $i); | |
} | |
branchTree($tree1, 12); | |
branchTree($tree2, 12); | |
branchTree($tree3, 12); | |
function crawlTreeAddingProperty(Tree $tree) : void { | |
if ($tree->left) { | |
if (rand(0, 1)) $tree->left->type = new Type(rand(0, 100)); | |
crawlTreeAddingProperty($tree->left); | |
} | |
if ($tree->right) { | |
if (rand(0, 1)) $tree->right->type = new Type(rand(0, 100)); | |
crawlTreeAddingProperty($tree->right); | |
} | |
} | |
function crawlTreeCheckingProperty(Tree $tree) : void { | |
if ($tree->left) { | |
if (isset($tree->left->type) && $tree->left->type->i < 0) { | |
echo 'never happens'; | |
} | |
crawlTreeCheckingProperty($tree->left); | |
} | |
if ($tree->right) { | |
if (isset($tree->right->type) && $tree->right->type->i < 0) { | |
echo 'never happens'; | |
} | |
crawlTreeCheckingProperty($tree->right); | |
} | |
} | |
function crawlTreeSettingObjectStorage(Tree $tree, SplObjectStorage $types) : void { | |
if ($tree->left) { | |
if (rand(0, 1)) $types[$tree->left] = new Type(rand(0, 100)); | |
crawlTreeSettingObjectStorage($tree->left, $types); | |
} | |
if ($tree->right) { | |
if (rand(0, 1)) $types[$tree->right] = new Type(rand(0, 100)); | |
crawlTreeSettingObjectStorage($tree->right, $types); | |
} | |
} | |
function crawlTreeGettingObjectStorage(Tree $tree, SplObjectStorage $types) : void { | |
if ($tree->left) { | |
if (isset($types[$tree->left]) && $types[$tree->left]->i < 0) { | |
echo 'never happens'; | |
} | |
crawlTreeGettingObjectStorage($tree->left, $types); | |
} | |
if ($tree->right) { | |
if (isset($types[$tree->right]) && $types[$tree->right]->i < 0) { | |
echo 'never happens'; | |
} | |
crawlTreeGettingObjectStorage($tree->right, $types); | |
} | |
} | |
function crawlTreeSettingContext(Tree $tree, Context $context) : void { | |
if ($tree->left) { | |
if (rand(0, 1)) $context->setType(spl_object_id($tree->left), new Type(rand(0, 100))); | |
crawlTreeSettingContext($tree->left, $context); | |
} | |
if ($tree->right) { | |
if (rand(0, 1)) $context->setType(spl_object_id($tree->right), new Type(rand(0, 100))); | |
crawlTreeSettingContext($tree->right, $context); | |
} | |
} | |
function crawlTreeGettingContext(Tree $tree, Context $context) : void { | |
if ($tree->left) { | |
$type = $context->getType(spl_object_id($tree->left)); | |
if ($type !== null && $type->i < 0) { | |
echo 'never happens'; | |
} | |
crawlTreeGettingContext($tree->left, $context); | |
} | |
if ($tree->right) { | |
$type = $context->getType(spl_object_id($tree->right)); | |
if ($type !== null && $type->i < 0) { | |
echo 'never happens'; | |
} | |
crawlTreeGettingContext($tree->right, $context); | |
} | |
} | |
$time = microtime(true); | |
crawlTreeAddingProperty($tree1); | |
crawlTreeCheckingProperty($tree1); | |
echo 1000 * (microtime(true) - $time) . PHP_EOL; | |
$time = microtime(true); | |
$types = new SplObjectStorage(); | |
crawlTreeSettingObjectStorage($tree2, $types); | |
crawlTreeGettingObjectStorage($tree2, $types); | |
echo 1000 * (microtime(true) - $time) . PHP_EOL; | |
$time = microtime(true); | |
$context = new Context(); | |
crawlTreeSettingContext($tree3, $context); | |
crawlTreeGettingContext($tree3, $context); | |
echo 1000 * (microtime(true) - $time) . PHP_EOL; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment