Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Roblox
GitHub Repository: Roblox/luau
Path: blob/master/bench/tests/shootout/binary-trees.lua
2727 views
1
--[[
2
MIT License
3
4
Copyright (c) 2017 Gabriel de Quadros Ligneul
5
6
Permission is hereby granted, free of charge, to any person obtaining a copy
7
of this software and associated documentation files (the "Software"), to deal
8
in the Software without restriction, including without limitation the rights
9
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
copies of the Software, and to permit persons to whom the Software is
11
furnished to do so, subject to the following conditions:
12
13
The above copyright notice and this permission notice shall be included in all
14
copies or substantial portions of the Software.
15
16
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
SOFTWARE.
23
]]
24
-- The Computer Language Benchmarks Game
25
-- http://benchmarksgame.alioth.debian.org/
26
-- contributed by Mike Pall
27
28
local function prequire(name) local success, result = pcall(require, name); return success and result end
29
local bench = script and require(script.Parent.bench_support) or prequire("bench_support") or require("../../bench_support")
30
31
function test()
32
33
local function BottomUpTree(item, depth)
34
if depth > 0 then
35
local i = item + item
36
depth = depth - 1
37
local left, right = BottomUpTree(i-1, depth), BottomUpTree(i, depth)
38
return { item, left, right }
39
else
40
return { item }
41
end
42
end
43
44
local function ItemCheck(tree)
45
if tree[2] then
46
return tree[1] + ItemCheck(tree[2]) - ItemCheck(tree[3])
47
else
48
return tree[1]
49
end
50
end
51
52
local N = tonumber(arg and arg[1]) or 10
53
54
local mindepth = 4
55
local maxdepth = mindepth + 2
56
if maxdepth < N then maxdepth = N end
57
58
do
59
local stretchdepth = maxdepth + 1
60
local stretchtree = BottomUpTree(0, stretchdepth)
61
print(string.format("stretch tree of depth %d\t check: %d\n",
62
stretchdepth, ItemCheck(stretchtree)))
63
end
64
65
local longlivedtree = BottomUpTree(0, maxdepth)
66
67
for depth=mindepth,maxdepth,2 do
68
local iterations = 2 ^ (maxdepth - depth + mindepth)
69
local check = 0
70
for i=1,iterations do
71
check = check + ItemCheck(BottomUpTree(1, depth)) +
72
ItemCheck(BottomUpTree(-1, depth))
73
end
74
print(string.format("%d\t trees of depth %d\t check: %d\n",
75
iterations*2, depth, check))
76
end
77
78
print(string.format("long lived tree of depth %d\t check: %d\n",
79
maxdepth, ItemCheck(longlivedtree)))
80
81
end
82
83
bench.runCode(test, "binary-trees")
84
85