lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


> That's awesome! Is that benchmark program public? I'm curious if armv7
> based iOS and Android devices will show the same behavior.

It was not anything serious; but I am attaching them to this message.
The only one I changed for Lua 3.0 was the heapsort, where I added
the "floor = math.ifloor or floor" line.

-- Roberto
 -- $Id: ackermann.lua,v 1.5 2000/12/09 20:07:43 doug Exp $
 -- http://www.bagley.org/~doug/shootout/

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

 N = tonumber((arg and arg[1])) or 3
 M = tonumber((arg and arg[2])) or 8
 print(string.format("Ack(%d, %d) = %d\n", N, M, Ack(N,M)))
local random, floor = math.random, math.floor
floor = math.ifloor or floor

function heapsort(n, ra)
    local j, i, rra
    local l = floor(n/2) + 1
    -- local l = (n//2) + 1
    local ir = n;
    while 1 do
        if l > 1 then
            l = l - 1
            rra = ra[l]
        else
            rra = ra[ir]
            ra[ir] = ra[1]
            ir = ir - 1
            if (ir == 1) then
                ra[1] = rra
                return
            end
        end
        i = l
        j = l * 2
        while j <= ir do
            if (j < ir) and (ra[j] < ra[j+1]) then
                j = j + 1
            end
            if rra < ra[j] then
                ra[i] = ra[j]
                i = j
                j = j + i
            else
                j = ir + 1
            end
        end
        ra[i] = rra
    end
end

local Num = tonumber((arg and arg[1])) or 4
for i=1,Num do
  local N = tonumber((arg and arg[2])) or 10000
  local a = {}
  for i=1,N do a[i] = random() end
  heapsort(N, a)
  for i=1,N-1 do assert(a[i] <= a[i+1]) end
end

local Complex={type="package"}

local function complex(x,y)
 return setmetatable({ re=x, im=y }, Complex.metatable)
end

function Complex.conj(x,y)
 return complex(x.re,-x.im)
end

function Complex.norm2(x)
 local n=Complex.mul(x,Complex.conj(x))
 return n.re
end

function Complex.abs(x)
 return sqrt(Complex.norm2(x))
end

function Complex.add(x,y)
 return complex(x.re+y.re,x.im+y.im)
end

function Complex.mul(x,y)
 return complex(x.re*y.re-x.im*y.im,x.re*y.im+x.im*y.re)
end

Complex.metatable={
	__add = Complex.add,
	__mul = Complex.mul,
}

local function abs(x)
 return math.sqrt(Complex.norm2(x))
end

xmin=-2		xmax=2		ymin=-2		ymax=2
N=256

function level(x,y)
 local c=complex(x,y)
 local l=0
 local z=c
 repeat
  z=z*z+c
  l=l+1
 until abs(z)>2.0 or l>255
 return l-1
end

dx=(xmax-xmin)/N
dy=(ymax-ymin)/N

print("P2")
print("# mandelbrot set",xmin,xmax,ymin,ymax,N)
print(N,N,255)
local S = 0
for i=1,N do
 local x=xmin+(i-1)*dx
 for j=1,N do
  local y=ymin+(j-1)*dy
  S = S + level(x,y)
 end
end
print(S)
-- $Id: matrix.lua,v 1.3 2001/07/11 17:27:49 doug Exp $
-- http://www.bagley.org/~doug/shootout/
-- with help from Roberto Ierusalimschy

local size = tonumber((arg and arg[1]) or 50) 

function mkmatrix(rows, cols)
    local mx = {}
    for i=0,(rows - 1) do
	local row = {}
	for j=0,(cols - 1) do
	    row[j] = math.random()
	end
	mx[i] = row
    end
    return(mx)
end

function mmult(rows, cols, m1, m2)
    local m3 = {}
    for i=0,(rows-1) do
        m3[i] = {}
        local m1_i = m1[i]              -- "cache" m1[i]
        for j=0,(cols-1) do
            local rowj = 0
            for k=0,(cols-1) do
                rowj = rowj + m1_i[k] * m2[k][j]
            end
            m3[i][j] = rowj
        end
    end
    return(m3)
end

local m1 = mkmatrix(size, size)
local m2 = mkmatrix(size, size)
mm = mmult(size, size, m1, m2)
io.write(string.format("%g %g %g %g\n", mm[0][0], mm[2][3], mm[3][2], mm[4][4]))