1
0
forked from Mirror/wren

Add JS version of binary tree benchmark.

This commit is contained in:
Bob Nystrom
2013-11-29 20:25:00 -08:00
parent f6c9848706
commit 74c414937c
2 changed files with 76 additions and 10 deletions

54
benchmark/binary_trees.js Normal file
View File

@ -0,0 +1,54 @@
/* The Great Computer Language Shootout
http://shootout.alioth.debian.org/
contributed by Isaac Gouy */
function TreeNode(left, right, item) {
this.left = left;
this.right = right;
this.item = item;
}
TreeNode.prototype.itemCheck = function() {
if (this.left == null) return this.item;
return this.item + this.left.itemCheck() - this.right.itemCheck();
}
function bottomUpTree(item, depth) {
if (depth > 0) {
return new TreeNode(
bottomUpTree(2 * item - 1, depth - 1),
bottomUpTree(2 * item, depth - 1), item);
}
return new TreeNode(null, null, item);
}
var minDepth = 4;
var maxDepth = 14;
var stretchDepth = maxDepth + 1;
var start = process.hrtime();
var check = bottomUpTree(0, stretchDepth).itemCheck();
console.log("stretch tree of depth " + stretchDepth + "\t check: " + check);
var longLivedTree = bottomUpTree(0, maxDepth);
for (var depth = minDepth; depth <= maxDepth; depth += 2) {
var iterations = 1 << (maxDepth - depth + minDepth);
check = 0;
for (var i = 1; i <= iterations; i++) {
check += bottomUpTree(i, depth).itemCheck();
check += bottomUpTree(-i, depth).itemCheck();
}
console.log(iterations * 2 + "\t trees of depth " + depth +
"\t check: " + check);
}
console.log("long lived tree of depth " + maxDepth + "\t check: "
+ longLivedTree.itemCheck());
var elapsed = process.hrtime(start);
elapsed = elapsed[0] + elapsed[1] / 1000000000;
console.log("elapsed: " + elapsed);

View File

@ -30,15 +30,15 @@ elapsed: (\d+\.\d+)""")
LANGUAGES = [
("wren", "../build/Release/wren", ".wren"),
("js", "node", ".js"),
("lua", "lua", ".lua"),
("python", "python", ".py"),
("ruby", "ruby", ".rb"),
("js", "node", ".js")
("ruby", "ruby", ".rb")
]
# How many times to run a given benchmark. Should be an odd number to get the
# right median.
NUM_TRIALS = 7
NUM_TRIALS = 5
results = []
@ -65,7 +65,8 @@ def run_benchmark_once(benchmark, language):
def run_benchmark(benchmark, language):
print "{0} - {1:10s}".format(benchmark[0], language[0]),
name = "{0} - {1}".format(benchmark[0], language[0])
print "{0:22s}".format(name),
if not os.path.exists(benchmark[0] + language[2]):
print "No implementation for this language"
@ -84,26 +85,37 @@ def run_benchmark(benchmark, language):
print " mean: {0:.4f} median: {1:.4f} std_dev: {2:.4f}".format(
stats[0], stats[1], stats[2])
results.append([benchmark[0] + " - " + language[0], times])
results.append([name, times])
def graph_results():
print
INCREMENT = {
'-': 'o',
'o': 'O',
'O': '0',
'0': '0'
}
# Scale everything by the highest time.
highest = 0
for result in results:
time = max(result[1])
if time > highest: highest = time
print "{0:24s} 0.0 {1:76.4f}".format("", highest)
print "{0:22s}0.0 {1:64.4f}".format("", highest)
for result in results:
line = ["-"] * 80
line = ["-"] * 68
for time in result[1]:
line[int(time / highest * 79)] = "O"
print "{0:24s} {1}".format(result[0], "".join(line))
index = int(time / highest * 67)
line[index] = INCREMENT[line[index]]
print "{0:22s}{1}".format(result[0], "".join(line))
print
for benchmark in BENCHMARKS:
for language in LANGUAGES:
run_benchmark(benchmark, language)
graph_results()
results = []
graph_results()