binary-trees Hack #2 program
source code
<?hh
/* The Computer Language Benchmarks Game
http://benchmarksgame.alioth.debian.org/
contributed by Peter Baltruschat
modified by Levi Cameron
modified by Craig Russell
PHP as HHVM/Hack by Isaac Gouy
*reset*
*/
class Tree {
public $l;
public $r;
public function __construct($depth) {
if($depth) {
$this->l = new Tree(--$depth);
$this->r = new Tree($depth);
}
}
public function check() {
return 1
+ ($this->l->l === null ? 1 : $this->l->check())
+ ($this->r->l === null ? 1 : $this->r->check());
}
}
$minDepth = 4;
$n = $argc == 2 ? $argv[1] : 1;
$maxDepth = $minDepth + 2 > $n ? $minDepth + 2 : $n;
$stretchDepth = $maxDepth + 1;
$stretch = new Tree($stretchDepth);
printf("stretch tree of depth %d\t check: %d\n",
$stretchDepth, $stretch->check());
unset($stretch);
$longLivedTree = new Tree($maxDepth);
$iterations = 1 << $maxDepth;
do
{
$check = 0;
for($i = 1; $i <= $iterations; ++$i)
{
$check += (new Tree($minDepth))->check();
}
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, $longLivedTree->check());
notes, command-line, and program output
NOTES:
64-bit Ubuntu quad core
HipHop VM 3.21.0 (rel)
Compiler: 3.21.0+dfsg-2
Repo schema: 1c159cf2047dca5f4a3363b2138a33e14a1e99fa
Wed, 15 Nov 2017 22:10:02 GMT
MAKE:
/usr/bin/hh_client
No errors!
0.06s to complete and log all make actions
COMMAND LINE:
/usr/bin/hhvm -d hhvm.hack.lang.look_for_typechecker=0 binarytrees.hack-2.hack 21
PROGRAM OUTPUT:
stretch tree of depth 22 check: 8388607
2097152 trees of depth 4 check: 65011712
524288 trees of depth 6 check: 66584576
131072 trees of depth 8 check: 66977792
32768 trees of depth 10 check: 67076096
8192 trees of depth 12 check: 67100672
2048 trees of depth 14 check: 67106816
512 trees of depth 16 check: 67108352
128 trees of depth 18 check: 67108736
32 trees of depth 20 check: 67108832
long lived tree of depth 21 check: 4194303