Skip to content

Commit

Permalink
feat(Lua): add some benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
leon0399 committed Jun 30, 2024
1 parent 1f9090e commit 0ff6584
Show file tree
Hide file tree
Showing 6 changed files with 278 additions and 0 deletions.
13 changes: 13 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,17 @@ RUN wget --progress=dot:giga -O - \
https://download.swift.org/swift-${SWIFT}-release/debian12/swift-${SWIFT}-RELEASE/swift-${SWIFT}-RELEASE-debian12.tar.gz | tar -xz
ENV PATH="/opt/swift-${SWIFT}-RELEASE-debian12/usr/bin:${PATH}"

# Lua
RUN apt install -y \
lua5.4 \
luajit

# C#
RUN wget https://packages.microsoft.com/config/debian/12/packages-microsoft-prod.deb -O packages-microsoft-prod.deb \
&& dpkg -i packages-microsoft-prod.deb \
&& rm packages-microsoft-prod.deb \
&& apt update \
&& apt install -y \
dotnet-sdk-8.0 aspnetcore-runtime-8.0 dotnet-runtime-8.0 mono-complete

WORKDIR /app
17 changes: 17 additions & 0 deletions lua/benchmark.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
title: 'Lua'

strategy:
matrix:
command:
- title: 'Lua'
command: 'lua %s'
- title: 'LuaJIT'
command: 'luajit %s'
tags:
- JIT

files:
- primes/Simple.lua
- treap/Naive.lua
- recursion/Tak.lua
- mandelbrot/Simple.lua
63 changes: 63 additions & 0 deletions lua/mandelbrot/Simple.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/usr/bin/env lua

function index()
for y = -39, 38 do
io.write("\n")

for x = -39, 38 do
local i = mandelbrot(
x / 40.0,
y / 40.0
)

if i == 0 then
io.write("*")
else
io.write(" ")
end
end
end

io.write("\n")
end

function mandelbrot(x, y)
local cr = y - 0.5
local ci = x
local zi = 0.0
local zr = 0.0
local i = 0

while true do
i = i + 1

local temp = zr * zi

local zr2 = zr * zr
local zi2 = zi * zi

zr = zr2 - zi2 + cr
zi = temp + temp + ci

if zi2 + zr2 > 16 then
return i
end

if i > 5000 then
return 0
end
end
end

local function main()
local startTime = os.clock()

index()

local endTime = os.clock()
local duration = (endTime - startTime) * 1000

print(string.format("Execution time: %.0fms", duration))
end

main()
40 changes: 40 additions & 0 deletions lua/primes/Simple.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/usr/bin/env lua

local NUMBER = 1000000

local function getLastPrime(count)
local lastPrime = 2

-- Traverse each number from 3 to N, skipping even numbers
for i = 3, count, 2 do
local isPrime = true
local limit = math.floor(math.sqrt(i))

for j = 3, limit, 2 do
if i % j == 0 then
isPrime = false
break
end
end

if isPrime then
lastPrime = i
end
end

return lastPrime
end

local function main()
local startTime = os.clock()

local lastPrime = getLastPrime(NUMBER)
print("Last prime: " .. lastPrime)

local endTime = os.clock()
local durationMs = (endTime - startTime) * 1000

print("Execution time: " .. durationMs .. "ms")
end

main()
20 changes: 20 additions & 0 deletions lua/recursion/Tak.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
local function tak(x, y, z)
if y < x then
return tak(tak(x - 1, y, z), tak(y - 1, z, x), tak(z - 1, x, y))
else
return z
end
end

local function benchmark()
local startTime = os.clock() * 1000

print(tak(30, 22, 12))

local endTime = os.clock() * 1000
local duration = endTime - startTime

print("Execution time: " .. duration .. "ms")
end

benchmark()
125 changes: 125 additions & 0 deletions lua/treap/Naive.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
math.randomseed(os.time())

Node = {}
Node.__index = Node

function Node:new(x)
local instance = setmetatable({}, self)
instance.x = x
instance.y = math.random()
instance.left = nil
instance.right = nil
return instance
end

function merge(lower, greater)
if lower == nil then
return greater
end

if greater == nil then
return lower
end

if lower.y < greater.y then
lower.right = merge(lower.right, greater)
return lower
else
greater.left = merge(lower, greater.left)
return greater
end
end

function splitBinary(orig, value)
if orig == nil then
return nil, nil
end

if orig.x < value then
local rightLower, rightGreater = splitBinary(orig.right, value)
orig.right = rightLower
return orig, rightGreater
else
local leftLower, leftGreater = splitBinary(orig.left, value)
orig.left = leftGreater
return leftLower, orig
end
end

function merge3(lower, equal, greater)
return merge(merge(lower, equal), greater)
end

SplitResult = {}
SplitResult.__index = SplitResult

function SplitResult:new(lower, equal, greater)
local instance = setmetatable({}, self)
instance.lower = lower
instance.equal = equal
instance.greater = greater
return instance
end

function split(orig, value)
local lower, equalGreater = splitBinary(orig, value)
local equal, greater = splitBinary(equalGreater, value + 1)
return SplitResult:new(lower, equal, greater)
end

Tree = {}
Tree.__index = Tree

function Tree:new()
local instance = setmetatable({}, self)
instance.root = nil
return instance
end

function Tree:hasValue(x)
local splited = split(self.root, x)
local res = splited.equal ~= nil
self.root = merge3(splited.lower, splited.equal, splited.greater)
return res
end

function Tree:insert(x)
local splited = split(self.root, x)
if splited.equal == nil then
splited.equal = Node:new(x)
end
self.root = merge3(splited.lower, splited.equal, splited.greater)
end

function Tree:erase(x)
local splited = split(self.root, x)
self.root = merge(splited.lower, splited.greater)
end

function main()
local tree = Tree:new()
local cur = 5
local res = 0

for i = 1, 999999 do
local a = i % 3
cur = (cur * 57 + 43) % 10007

if a == 0 then
tree:insert(cur)
elseif a == 1 then
tree:erase(cur)
elseif a == 2 then
res = res + (tree:hasValue(cur) and 1 or 0)
end
end

print(res)
end

local startTimeMs = math.floor(os.clock() * 1000)
main()
local endTimeMs = math.floor(os.clock() * 1000)
local durationMs = endTimeMs - startTimeMs

print("Execution time: " .. durationMs .. "ms")

0 comments on commit 0ff6584

Please sign in to comment.