The Computer Language
Benchmarks Game

binary-trees Scala #5 program

source code

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

   contributed by Kannan Goundan
   modified by Isaac Gouy
   optimized by David Pollak
   updated to 2.8 by Rex Kerr
   parallelized by Yang Bo
   *reset*
*/
 
import scala.concurrent.duration._
import scala.concurrent._
import scala.concurrent.ExecutionContext.Implicits.global 
object binarytrees {
  def main(args: Array[String]) = {
    val n = try{ args(0).toInt } catch { case _ => 1 }
    val minDepth = 4
    val maxDepth = n max (minDepth+2)
 
    def print(name: String, depth: Int, check: Int) =
      println(name + " of depth " + depth + "\t check: " + check)
 
    print("stretch tree", maxDepth+1, Await.result(Tree(maxDepth+1), Duration.Inf).isum)
    val longLivedTree = Await.result(Tree(maxDepth), Duration.Inf)
    var depth = minDepth
    while (depth <= maxDepth) {
      val iterations = 1 << (maxDepth - depth + minDepth)
      var i,sum = 0
      while (i < iterations) {
        i += 1
        sum += Await.result(Tree(depth), Duration.Inf).isum
      }
      print(iterations + "\t trees", depth, sum)
      depth += 2
    }
    print("long lived tree", maxDepth, longLivedTree.isum)
  }
}
 
final class Tree(left: Tree, right: Tree) {
  def isum: Int = {
    val tl = left
    if (tl eq null) 1
    else 1 + tl.isum + right.isum
  }
}
object Tree {
  def apply(depth: Int, futureDepth: Int = 0): Future[Tree] = {
    if (futureDepth < 4) {
      if (depth > 0) 
         Tree(depth-1, futureDepth+1).zip(Tree(depth-1, futureDepth+1)).map { 
            case (left, right) => new Tree(left, right) }
      else 
         Future(new Tree(null, null))
    } else {
      def synchronizedApply(depth: Int):Tree = {
        if (depth > 0) new Tree(synchronizedApply(depth-1), synchronizedApply(depth-1))
        else new Tree(null, null)
      }
      Future(synchronizedApply(depth))
    }
  }
}
    

notes, command-line, and program output

NOTES:
64-bit Ubuntu quad core
Scala compiler version 2.12.1 -- Copyright 2002-2016, LAMP/EPFL and Lightbend, Inc.
java version "1.8.0_121"
Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)


Sat, 25 Mar 2017 01:40:20 GMT

MAKE:
mv binarytrees.scala-5.scala binarytrees.scala
/usr/local/src/scala-2.12.1/bin/scalac -optimise -target:jvm-1.8 binarytrees.scala
binarytrees.scala:17: warning: This catches all Throwables. If this is really intended, use `case _ : Throwable` to clear this warning.
    val n = try{ args(0).toInt } catch { case _ => 1 }
                                              ^
warning: there was one deprecation warning; re-run with -deprecation for details
two warnings found
5.82s to complete and log all make actions

COMMAND LINE:
 /usr/local/src/jdk1.8.0_121/bin/java  -Xbootclasspath/a:/usr/local/src/scala-2.12.1/lib/scala-library.jar:/usr/local/src/scala-2.12.1/lib/scala-actors-2.11.0.jar:/usr/local/src/scala-2.12.1/lib/akka-actor_2.11-2.3.4.jar:/usr/local/src/scala-2.12.1/lib/config-1.2.1.jar binarytrees 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