performance measurements

Each table row shows performance measurements for this Go program with a particular command-line input value N.

 N  CPU secs Elapsed secs Memory KB Code B ≈ CPU Load
120.210.22764567  0% 5% 4% 100%
164.494.4918,824567  1% 2% 0% 100%
2092.6892.79290,256567  1% 1% 1% 100%

Read the ↓ make, command line, and program output logs to see how this program was run.

Read binary-trees benchmark to see what this program should do.

 notes

go version go1.4 linux/amd64

 binary-trees Go #7 program source code

/* The Computer Language Benchmarks Game
 * http://benchmarksgame.alioth.debian.org/
 *
 * contributed by Anthony Perez-Sanz.
 * based on Java program by Jarkko Miettinen
 */

package main

import (
   "flag"
   "fmt"
   "strconv"
)

type Node struct {
   item        int
   left, right *Node
}

const minDepth = 4

func trees(maxDepth int) {
   longLastingNode := createTree(0, maxDepth)
   depth := 4

   for depth <= maxDepth {
      iterations := 1 << uint(maxDepth-depth+minDepth) // 16 << (maxDepth - depth)

      loops(iterations, depth)
      depth += 2
   }
   fmt.Printf("long lived tree of depth %d\t check: %d\n", maxDepth,
      checkTree(longLastingNode))
}

func loops(iterations, depth int) {
   check := 0
   item := 0
   for item < iterations {
      check += checkTree(createTree(item, depth)) +
         checkTree(createTree(-item, depth))
      item++
   }
   fmt.Printf("%d\t trees of depth %d\t check: %d\n",
      iterations<<1, depth, check)
}

func checkTree(n *Node) int {
   if n.left == nil {
      return n.item
   }
   return checkTree(n.left) - checkTree(n.right) + n.item
}

func createTree(item, depth int) *Node {
   node := &Node{item: item}
   if depth > 0 {
      item += item
      depth--
      node.left = createTree(item-1, depth)
      node.right = createTree(item, depth)
   }
   return node
}

func main() {
   n := 0
   flag.Parse()
   if flag.NArg() > 0 {
      n, _ = strconv.Atoi(flag.Arg(0))
   }

   maxDepth := n
   if minDepth+2 > n {
      maxDepth = minDepth + 2
   }

   {
      stretchDepth := maxDepth + 1
      check := checkTree(createTree(0, stretchDepth))
      fmt.Printf("stretch tree of depth %d\t check: %d\n", stretchDepth, check)
   }
   trees(maxDepth)
}

 make, command-line, and program output logs

Thu, 11 Dec 2014 20:16:31 GMT

MAKE:
/usr/local/src/go/bin/go build -o binarytrees.go-7.go_run
0.50s to complete and log all make actions

COMMAND LINE:
./binarytrees.go-7.go_run 20

PROGRAM OUTPUT:
stretch tree of depth 21	 check: -1
2097152	 trees of depth 4	 check: -2097152
524288	 trees of depth 6	 check: -524288
131072	 trees of depth 8	 check: -131072
32768	 trees of depth 10	 check: -32768
8192	 trees of depth 12	 check: -8192
2048	 trees of depth 14	 check: -2048
512	 trees of depth 16	 check: -512
128	 trees of depth 18	 check: -128
32	 trees of depth 20	 check: -32
long lived tree of depth 20	 check: -1

Revised BSD license

  Home   Conclusions   License   Play