-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlua_benchmark.lua
182 lines (164 loc) · 3.69 KB
/
lua_benchmark.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
-- done with Copilot AI
-- Function to measure execution time
local function measurePerformance(func, ...)
local startTime = os.clock()
func(...)
local endTime = os.clock()
return endTime - startTime
end
local function fibonacci(n)
if n <= 1 then
return n
else
return fibonacci(n - 1) + fibonacci(n - 2)
end
end
local function partition(arr, low, high)
local pivot = arr[high]
local i = low - 1
for j = low, high - 1 do
if arr[j] <= pivot then
i = i + 1
arr[i], arr[j] = arr[j], arr[i]
end
end
arr[i + 1], arr[high] = arr[high], arr[i + 1]
return i + 1
end
local function quicksort(arr, low, high)
if low < high then
local pi = partition(arr, low, high)
quicksort(arr, low, pi - 1)
quicksort(arr, pi + 1, high)
end
end
local function matrixMultiply(A, B)
local C = {}
for i = 1, #A do
C[i] = {}
for j = 1, #B[1] do
C[i][j] = 0
for k = 1, #B do
C[i][j] = C[i][j] + A[i][k] * B[k][j]
end
end
end
return C
end
-- BINARY TREE
-- Define a node of the binary tree
local Node = {}
Node.__index = Node
function Node:new(value)
local node = {
value = value,
left = nil,
right = nil
}
setmetatable(node, Node)
return node
end
-- Define the binary tree
local BinaryTree = {}
BinaryTree.__index = BinaryTree
function BinaryTree:new()
local tree = {
root = nil
}
setmetatable(tree, BinaryTree)
return tree
end
function BinaryTree:insert(value)
local newNode = Node:new(value)
if self.root == nil then
self.root = newNode
else
self:_insertNode(self.root, newNode)
end
end
function BinaryTree:_insertNode(currentNode, newNode)
if newNode.value < currentNode.value then
if currentNode.left == nil then
currentNode.left = newNode
else
self:_insertNode(currentNode.left, newNode)
end
else
if currentNode.right == nil then
currentNode.right = newNode
else
self:_insertNode(currentNode.right, newNode)
end
end
end
function BinaryTree:search(value)
return self:_searchNode(self.root, value)
end
function BinaryTree:_searchNode(currentNode, value)
if currentNode == nil then
return false
end
if value == currentNode.value then
return true
elseif value < currentNode.value then
return self:_searchNode(currentNode.left, value)
else
return self:_searchNode(currentNode.right, value)
end
end
-- Create a binary tree and measure performance of insertions
function sfence_help.lua_benchmark(seed, runs, log_func)
math.randomseed(seed)
log_func("Start Lua benchmark for seed "..seed.." with "..runs.." runs.")
local results = {}
log_func("Start fibonacci")
results["fibonacci"] = measurePerformance(function()
for i = 1, runs do
fibonacci(math.random(1, 20))
end
end)
-- quick sort
log_func("Start quicksort")
results["quicksort"] = measurePerformance(function()
for i = 1, runs do
local array = {}
for i = 1, 1000 do
array[i] = math.random(1, 10000)
end
quicksort(array, 1, #array)
end
end)
-- multiply matrix
log_func("Start multiply matrix")
results["matrixMultiply"] = measurePerformance(function()
for i = 1, runs do
local mA = {}
local mB = {}
for i = 1, 10 do
mA[i] = {}
mB[i] = {}
for j = 1, 10 do
mA[i][j] = math.random(1, 10000)
mB[i][j] = math.random(1, 10000)
end
end
matrixMultiply(mA, mB)
end
end)
-- binary tree
log_func("Start binary tree")
local tree = BinaryTree:new()
results["tree_insert"] = measurePerformance(function()
for i = 1, runs do
tree:insert(math.random(1, 10000))
end
end)
-- Measure performance of search operations
results["tree_search"] = measurePerformance(function()
for i = 1, runs do
tree:search(math.random(1, 10000))
end
end)
log_func("End of Lua benchmark")
return results
end