The Computer Language
Benchmarks Game

binary-trees PHP #4 program

source code

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

   contributed by Peter Baltruschat
   modified by Levi Cameron
   multiprocessing by Yuriy Moskalchuk
*/

function createTree($depth)
{
    if (!$depth) {
        return [null, null];
    }
    $depth--;

    return [
        createTree($depth),
        createTree($depth)
    ];
}

function checkTree($treeNode)
{
    return 1
        + ($treeNode[0][0] === null ? 1 : checkTree($treeNode[0]))
        + ($treeNode[1][0] === null ? 1 : checkTree($treeNode[1]));
}

$minDepth = 4;
$n = ($argc == 2) ? $argv[1] : 1;
$maxDepth = max($minDepth + 2, $n);
$stretchDepth = $maxDepth + 1;

$stretchTree = createTree($stretchDepth);
echo "stretch tree of depth $stretchDepth\t check: ", checkTree($stretchTree), PHP_EOL;
unset($stretchTree);

$longLivedTree = createTree($maxDepth);

function startWorker($depth, $iter, $n, $size, $shmId)
{
    $pid = pcntl_fork();
    if ($pid) {
        return $pid;
    } else {
        $check = 0;
        for ($i = 1; $i <= $iter; ++$i) {
            $t = createTree($depth);
            $check += checkTree($t);
            unset($t);
        }
        $result = "$iter\t trees of depth $depth\t check: $check\n";
        shmop_write($shmId, $result, $size * ($n - 1));
        exit(0);
    }
}

$iterations = 1 << ($maxDepth);
$depthIterations = [$minDepth => $iterations];
do {
    $minDepth += 2;
    if ($minDepth <= $maxDepth) {
        $iterations >>= 2;
        $depthIterations[$minDepth] = $iterations;
    }
} while ($minDepth <= $maxDepth);

extension_loaded('shmop') or dl('shmop.so');
$size = 128;
$shmId = shmop_open(ftok(__FILE__, 'b'), 'c', 0644, $size * (count($depthIterations) + 1));
$PIDs = [];
$n = 0;
foreach ($depthIterations as $d => $i) {
    $PIDs[] = startWorker($d, $i, ++$n, $size, $shmId);
}
foreach ($PIDs as $PID) {
    pcntl_waitpid($PID, $status);
}
foreach (range(1, count($depthIterations)) as $n) {
    echo trim(shmop_read($shmId, $size * ($n - 1), $size)), PHP_EOL;
}
shmop_delete($shmId);

echo "long lived tree of depth $maxDepth\t check: ", checkTree($longLivedTree), PHP_EOL;
    

notes, command-line, and program output

NOTES:
64-bit Ubuntu quad core
PHP 7.1.4 (cli) (built: Apr 16 2017 16:17:54) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies


Sun, 16 Apr 2017 23:45:58 GMT

COMMAND LINE:
/usr/local/src/php-7.1.4/bin/php -n -d memory_limit=4096M binarytrees.php-4.php 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