language-bench

Binary Trees

Allocate and walk millions of short-lived binary trees (depth 14).

gc-heavymemory-bounddata-structures

What is measured

Allocates and walks many short-lived binary trees (depth 14 settings from the Computer Language Benchmarks Game): a stretch tree, a long-lived tree, and for each depth 4, 6, ..., 14 many trees that are built, checked and discarded. There is no instance: every iteration runs the same workload. The check sums are the result.

Pseudocode

check(t) = 1 + check(t.left) + check(t.right)  (leaf = 1)
make(d)  = perfect binary tree of depth d

print "stretch tree of depth maxDepth+1", check(make(maxDepth+1))
longLived = make(maxDepth)
for depth = minDepth to maxDepth step 2:
    iterations = 2^(maxDepth - depth + minDepth)
    total = 0
    repeat iterations times:
        total += check(make(depth))      # short-lived, then garbage
    print iterations, depth, total
print "long lived tree", check(longLived)

Reveals: Allocation rate, pointer chasing and garbage-collector behaviour.

Caveat: Tree size, object layout and allocation lifetimes matter. C, C++ and Rust use plain malloc/new/Box (no arena); GC languages use their default collector. Peak memory differs widely by design.

Results (newest release of each language)

Language / buildTime (× vs best)
Go 1.27 · GOAMD64=v3 go build51.7 ms (1.00×)
Go 1.27 · go builddefault52.1 ms (1.01×)
C 16 · gcc -O359.5 ms (1.15×)
C 16 · gcc -O259.8 ms (1.16×)
C 16 · gcc -O3 -march=nativedefault60.2 ms (1.16×)
Python 3.15 · python3default288 ms (5.58×)

Click a language to see the source code, build flags, every session and how the number was computed.