-
Notifications
You must be signed in to change notification settings - Fork 146
/
Copy path1.php
64 lines (52 loc) · 1.36 KB
/
1.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
/* The Computer Language Benchmarks Game
https://salsa.debian.org/benchmarksgame-team/benchmarksgame/
contributed by Peter Baltruschat
modified by Levi Cameron
*reset*
*/
class Node
{
public $l;
public $r;
}
function bottomUpTree($depth)
{
$node = new Node();
if (!$depth) return $node;
$depth--;
$node->l = bottomUpTree($depth);
$node->r = bottomUpTree($depth);
return $node;
}
function itemCheck($treeNode)
{
return 1
+ ($treeNode->l->l === null ? 1 : itemCheck($treeNode->l))
+ ($treeNode->r->l === null ? 1 : itemCheck($treeNode->r));
}
$minDepth = 4;
$n = ($argc == 2) ? $argv[1] : 1;
$maxDepth = max($minDepth + 2, $n);
$stretchDepth = $maxDepth + 1;
$stretchTree = bottomUpTree($stretchDepth);
printf("stretch tree of depth %d\t check: %d\n", $stretchDepth, itemCheck($stretchTree));
unset($stretchTree);
$longLivedTree = bottomUpTree($maxDepth);
$iterations = 1 << ($maxDepth);
do {
$check = 0;
for ($i = 1; $i <= $iterations; ++$i) {
$t = bottomUpTree($minDepth);
$check += itemCheck($t);
unset($t);
}
printf("%d\t trees of depth %d\t check: %d\n", $iterations, $minDepth, $check);
$minDepth += 2;
$iterations >>= 2;
} while ($minDepth <= $maxDepth);
printf(
"long lived tree of depth %d\t check: %d\n",
$maxDepth,
itemCheck($longLivedTree)
);