Skip to content

Commit

Permalink
Separate lua benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
edubart committed Feb 15, 2020
1 parent 1e428c9 commit 1f1c6bd
Show file tree
Hide file tree
Showing 10 changed files with 171 additions and 16 deletions.
2 changes: 1 addition & 1 deletion benchmarks/heapsort.nelua
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ local function random_int(seed: integer)
end

local N = 1000000
local a: sequence(number)
local a: sequence(number) = {}
local rand = 123456789
for i=1,N do
rand = random_int(rand)
Expand Down
13 changes: 13 additions & 0 deletions benchmarks/lua/ackermann.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
local function ack(m, n)
if m == 0 then
return n + 1
end
if n == 0 then
return ack(m - 1, 1)
end
return ack(m - 1, ack(m, n - 1))
end

local res = ack(3,10)
print(res)
assert(res == 8189)
11 changes: 11 additions & 0 deletions benchmarks/lua/fibonacci.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
local function fibmod(n, m)
local a, b = 0, 1
for i=1,n do
a, b = b, (a + b) % m
end
return a
end

local res = fibmod(100000000, 1000000000000)
print(res)
assert(res == 167760546875)
57 changes: 57 additions & 0 deletions benchmarks/lua/heapsort.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
local function heapsort(a)
local n = #a
local j, i, t
local l = math.floor(n / 2) + 1
local k = n
while true do
if l > 1 then
l = l - 1
t = a[l]
else
t = a[k]
a[k] = a[1]
k = k - 1
if k == 1 then
a[1] = t
return
end
end
i = l
j = l * 2
while j <= k do
if j < k and a[j] < a[j+1] then
j = j + 1
end
if t < a[j] then
a[i] = a[j]
i = j
j = j + i
else
j = k + 1
end
end
a[i] = t
end
end

local function random_int(seed)
return (214013 * seed + 2531011) % 2147483648
end

local N = 1000000
local a = {}
local rand = 123456789
for i=1,N do
rand = random_int(rand)
a[i] = rand
end

heapsort(a)

local sum = 0.0
for i=1,N-1 do
assert(a[i] <= a[i+1])
sum = sum + (a[i+1] - a[i])
end
print(sum)
assert(sum == 2147480127.0)
43 changes: 43 additions & 0 deletions benchmarks/lua/mandel.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
local function mandel(width)
local height, wscale = width, 2.0/width
local m, limit2 = 50, 4.0
local sum = 0
for y=0,height-1 do
local Ci = 2.0*y / height - 1
for xb=0,width-1,8 do
local bits = 0
local xbb = xb+7
local xblimit
if xbb < width then
xblimit = xbb
else
xblimit = width-1
end
for x=xb,xblimit do
bits = bits + bits
local Zr, Zi, Zrq, Ziq = 0.0, 0.0, 0.0, 0.0
local Cr = x * wscale - 1.5
for i=1,m do
local Zri = Zr*Zi
Zr = Zrq - Ziq + Cr
Zi = Zri + Zri + Ci
Zrq = Zr*Zr
Ziq = Zi*Zi
if Zrq + Ziq > limit2 then
bits = bits + 1
break
end
end
end
if xbb >= width then
for x=width,xbb do bits = bits + bits + 1 end
end
sum = sum + bits
end
end
return sum
end

local res = mandel(1024)
print(res)
assert(res == 20164264)
21 changes: 21 additions & 0 deletions benchmarks/lua/sieve.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
local function sieve(N)
local is_prime = {}
is_prime[1] = false
for n=2,N do
is_prime[n] = true
end
local nprimes = 0
for n=2,N do
if is_prime[n] then
nprimes = nprimes + 1
for m=n+n,N,n do
is_prime[m] = false
end
end
end
return nprimes
end

local res = sieve(10000000)
print(res)
assert(res == 664579)
2 changes: 1 addition & 1 deletion benchmarks/sieve.nelua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'sequence'

local function sieve(N: integer)
local is_prime: sequence(boolean)
local is_prime: sequence(boolean) = {}
is_prime[1] = false
for n=2,N do
is_prime[n] = true
Expand Down
20 changes: 10 additions & 10 deletions docs/pages/nelua.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,17 +163,17 @@ by nelua compiler, as it can compile to Lua too.

The benchmarks can be run with `make benchmark`, this is my last results:

| benchmark | lua 5.3 | luajit 2.1 | nelua | c |
|--------------|------------|------------|------------|------------|
| ackermann | 2441.9 ms | 150.8 ms | 64.6 ms | 51.6 ms |
| fibonacci | 2607.6 ms | 934.4 ms | 387.6 ms | 319.7 ms |
| mandel | 2628.9 ms | 103.0 ms | 92.3 ms | 92.7 ms |
| sieve | 1252.8 ms | 282.0 ms | 98.7 ms | 70.7 ms |
| heapsort | 2680.6 ms | 298.4 ms | 186.5 ms | 145.7 ms |

*NOTE*: Nelua can match C speed if the benchmarks were coded using arrays instead of tables,
| benchmark | lua 5.3 | luajit 2.1 | nelua | c |
|--------------|----------|------------|----------|----------|
| ackermann | 2457.2 ms | 147.2 ms | 47.9 ms | 47.3 ms |
| fibonacci | 2612.4 ms | 951.7 ms | 400.4 ms | 323.2 ms |
| mandel | 2622.6 ms | 101.0 ms | 91.9 ms | 93.8 ms |
| sieve | 1266.1 ms | 298.9 ms | 101.9 ms | 71.7 ms |
| heapsort | 2792.1 ms | 287.7 ms | 171.3 ms | 142.2 ms |

*NOTE*: Nelua can match C speed if the benchmarks were coded using optimized structures,
however to make the benchmarks comparisons fair with Lua/LuaJIT they were coded in Lua style
(using tables and its API).
(using sequence tables and a garbage collector).

Environment that this benchmark was run:
LuaJIT 2.1.0-beta3,
Expand Down
10 changes: 10 additions & 0 deletions nelua/types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -916,6 +916,16 @@ function TableType:_init(name)
Type._init(self, name, cpusize)
end

--[[
function TableType.unary_operator()
return primtypes.any
end
function TableType.binary_operator()
return primtypes.any
end
]]

--------------------------------------------------------------------------------
local ArrayType = typeclass()
types.ArrayType = ArrayType
Expand Down
8 changes: 4 additions & 4 deletions tools/benchmarker.lua
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ end
local function run_benchmark(name)
benchmark(
string.format('| %12s | %9s', name, 'lua'),
string.format('lua ./nelua_cache/benchmarks/%s.lua', name),
string.format('lua ./benchmarks/lua/%s.lua', name),
config.ntimes)
benchmark(
string.format('| %12s | %9s', name, 'luajit'),
string.format('luajit ./nelua_cache/benchmarks/%s.lua', name),
string.format('luajit ./benchmarks/lua/%s.lua', name),
config.ntimes)
benchmark(
string.format('| %12s | %9s', name, 'nelua'),
Expand Down Expand Up @@ -96,8 +96,8 @@ local function c_compile(name)
end

local function compile_benchmark(name)
printf('%11s %s', name, 'nelua (lua)')
nelua_compile(name, 'lua')
--printf('%11s %s', name, 'nelua (lua)')
--nelua_compile(name, 'lua')
printf('%11s %s', name, 'nelua (c)')
nelua_compile(name, 'c')
printf('%11s %s', name, 'c')
Expand Down

0 comments on commit 1f1c6bd

Please sign in to comment.