summaryrefslogtreecommitdiff
path: root/lua_benchmark/tests/Lua-Benchmarks/binary-trees.lua
diff options
context:
space:
mode:
authorMatthew Sotoudeh <matthew@masot.net>2024-05-17 15:57:30 -0700
committerMatthew Sotoudeh <matthew@masot.net>2024-05-17 15:57:30 -0700
commitd068f0b3c11348a50c18af1ee3b0d2e5f38c4faf (patch)
treedb777acca2336f8c279e9f09346f02de7ddaa0e9 /lua_benchmark/tests/Lua-Benchmarks/binary-trees.lua
parent221b05e7a86faa38036429d5fbfc8b0779eb5382 (diff)
lua benchmarks
Diffstat (limited to 'lua_benchmark/tests/Lua-Benchmarks/binary-trees.lua')
-rw-r--r--lua_benchmark/tests/Lua-Benchmarks/binary-trees.lua51
1 files changed, 51 insertions, 0 deletions
diff --git a/lua_benchmark/tests/Lua-Benchmarks/binary-trees.lua b/lua_benchmark/tests/Lua-Benchmarks/binary-trees.lua
new file mode 100644
index 0000000..d7f52e3
--- /dev/null
+++ b/lua_benchmark/tests/Lua-Benchmarks/binary-trees.lua
@@ -0,0 +1,51 @@
+-- The Computer Language Benchmarks Game
+-- http://benchmarksgame.alioth.debian.org/
+-- contributed by Mike Pall
+
+local function BottomUpTree(item, depth)
+ if depth > 0 then
+ local i = item + item
+ depth = depth - 1
+ local left, right = BottomUpTree(i-1, depth), BottomUpTree(i, depth)
+ return { item, left, right }
+ else
+ return { item }
+ end
+end
+
+local function ItemCheck(tree)
+ if tree[2] then
+ return tree[1] + ItemCheck(tree[2]) - ItemCheck(tree[3])
+ else
+ return tree[1]
+ end
+end
+
+-- local N = tonumber(arg and arg[1]) or 0
+local N = 15
+local mindepth = 4
+local maxdepth = mindepth + 2
+if maxdepth < N then maxdepth = N end
+
+do
+ local stretchdepth = maxdepth + 1
+ local stretchtree = BottomUpTree(0, stretchdepth)
+ io.write(string.format("stretch tree of depth %d\t check: %d\n",
+ stretchdepth, ItemCheck(stretchtree)))
+end
+
+local longlivedtree = BottomUpTree(0, maxdepth)
+
+for depth=mindepth,maxdepth,2 do
+ local iterations = 2 ^ (maxdepth - depth + mindepth)
+ local check = 0
+ for i=1,iterations do
+ check = check + ItemCheck(BottomUpTree(1, depth)) +
+ ItemCheck(BottomUpTree(-1, depth))
+ end
+ io.write(string.format("%d\t trees of depth %d\t check: %d\n",
+ iterations*2, depth, check))
+end
+
+io.write(string.format("long lived tree of depth %d\t check: %d\n",
+ maxdepth, ItemCheck(longlivedtree)))
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback