The Computer Language
Benchmarks Game

binary-trees Hack program

source code

<?hh
/* The Computer Language Benchmarks Game
   http://benchmarksgame.alioth.debian.org/

   contributed by Peter Baltruschat
   modified by Levi Cameron
   PHP as HHVM/Hack by Isaac Gouy
   *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));

    

notes, command-line, and program output

NOTES:
64-bit Ubuntu quad core
HipHop VM 3.18.0 (rel)
Compiler: tags/HHVM-3.18.0-0-g9b285191feb2bb1558bb6682da135263bd2a9e60
Repo schema: cc987335f4691fa6ed98336c2378d8e71a751b22


Mon, 27 Mar 2017 21:37:07 GMT

MAKE:
/usr/bin/hh_client
For more detailed logs, try `tail -f $(hh_client --monitor-logname) $(hh_client --logname)`
Server launched with the following command:
	'/usr/bin/hh_server' '-d' '/home/dunham/benchmarksgame_quadcore' '--waiting-client' '7'
Spawned typechecker (child pid=3194)
Logs will go to /tmp/hh_server/zShomezSdunhamzSbenchmarksgame_quadcore.monitor_log
No errors!
1.15s to complete and log all make actions

COMMAND LINE:
/usr/bin/hhvm  binarytrees.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