The Developer's Library for D
 About Wiki Forums Source Search Contact

# GC Benchmark

The code and results below are meant to show the performance differences between Tango and Phobos, isolated to the garbage collector. Benchmark is contributed by Piotr Modzelewski.

## Code

This same code for both: Tango and Phobos test. (No need to import anything, GC is a part of the runtime).

```class TreeNode {
int item;
TreeNode left, right;

this(int item, TreeNode left=null, TreeNode right=null) {
this.item = item;
this.left = left;
this.right = right;
}

int check() {
return left is null ? item : item + left.check - right.check;
}
}

TreeNode makeTree(int item, int depth) {
if (depth > 0)
return new TreeNode(item, makeTree(2*item-1, depth-1), makeTree(2*item, depth-1));
else
return new TreeNode(item);
}

void main(char[][] args) {
const minDepth = 4;
int n = 16;
int maxDepth = (minDepth + 2) > n ? minDepth + 2 : n;

int check = makeTree(0, maxDepth + 1).check;

auto longLivedTree = makeTree(0, maxDepth);

for (int depth = minDepth; depth <= maxDepth; depth += 2) {
int iterations = 1 << (maxDepth - depth + minDepth);
check = 0;

for (int i = 1; i <= iterations; i++)
check += (makeTree(i, depth)).check + (makeTree(-i, depth)).check;
}

}
```

## Results

### Tango

```C:\coding\tangbench>rebuild -O -release -inline -full -cleanup bench.d
C:\coding\tangbench>sh
sh-2.04\$ time bench.exe

real    0m14.000s
user    0m0.010s
sys     0m0.010s
```

### Phobos

```C:\coding\tangbench>build -release -inline -O -full -cleanup benchphobos.d
C:\coding\tangbench>sh
sh-2.04\$ time ./benchphobos.exe

real    0m17.575s
user    0m0.010s
sys     0m0.010s
```