From d068f0b3c11348a50c18af1ee3b0d2e5f38c4faf Mon Sep 17 00:00:00 2001 From: Matthew Sotoudeh Date: Fri, 17 May 2024 15:57:30 -0700 Subject: lua benchmarks --- lua_benchmark/tests/Lua-Benchmarks/mandel.lua | 66 +++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 lua_benchmark/tests/Lua-Benchmarks/mandel.lua (limited to 'lua_benchmark/tests/Lua-Benchmarks/mandel.lua') diff --git a/lua_benchmark/tests/Lua-Benchmarks/mandel.lua b/lua_benchmark/tests/Lua-Benchmarks/mandel.lua new file mode 100644 index 0000000..bd080da --- /dev/null +++ b/lua_benchmark/tests/Lua-Benchmarks/mandel.lua @@ -0,0 +1,66 @@ +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.0 xmax=2.0 ymin=-2.0 ymax=2.0 +N=arg[1] or 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 + -- if i % 10 == 0 then print(collectgarbage"count") end +end +print(S) -- cgit v1.2.3