Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Roblox
GitHub Repository: Roblox/luau
Path: blob/master/bench/gc/test_BinaryTree.lua
2725 views
1
local function prequire(name) local success, result = pcall(require, name); return success and result end
2
local bench = script and require(script.Parent.bench_support) or prequire("bench_support") or require("../bench_support")
3
4
function test()
5
6
-- The Computer Language Benchmarks Game
7
-- http://benchmarksgame.alioth.debian.org/
8
-- contributed by Mike Pall
9
10
local function BottomUpTree(item, depth)
11
if depth > 0 then
12
local i = item + item
13
depth = depth - 1
14
local left, right = BottomUpTree(i-1, depth), BottomUpTree(i, depth)
15
return { item, left, right }
16
else
17
return { item }
18
end
19
end
20
21
local function ItemCheck(tree)
22
if tree[2] then
23
return tree[1] + ItemCheck(tree[2]) - ItemCheck(tree[3])
24
else
25
return tree[1]
26
end
27
end
28
29
local N = 10
30
local mindepth = 4
31
local maxdepth = mindepth + 2
32
if maxdepth < N then maxdepth = N end
33
34
local ts0 = os.clock()
35
36
do
37
local stretchdepth = maxdepth + 1
38
local stretchtree = BottomUpTree(0, stretchdepth)
39
end
40
41
local longlivedtree = BottomUpTree(0, maxdepth)
42
43
for depth=mindepth,maxdepth,2 do
44
local iterations = 2 ^ (maxdepth - depth + mindepth)
45
local check = 0
46
for i=1,iterations do
47
check = check + ItemCheck(BottomUpTree(1, depth)) +
48
ItemCheck(BottomUpTree(-1, depth))
49
end
50
end
51
52
local ts1 = os.clock()
53
54
return ts1 - ts0
55
end
56
57
bench.runCode(test, "BinaryTree")
58