diff options
author | Matthew Sotoudeh <matthew@masot.net> | 2024-05-17 15:57:30 -0700 |
---|---|---|
committer | Matthew Sotoudeh <matthew@masot.net> | 2024-05-17 15:57:30 -0700 |
commit | d068f0b3c11348a50c18af1ee3b0d2e5f38c4faf (patch) | |
tree | db777acca2336f8c279e9f09346f02de7ddaa0e9 /lua_benchmark/tests/Lua-Benchmarks | |
parent | 221b05e7a86faa38036429d5fbfc8b0779eb5382 (diff) |
lua benchmarks
Diffstat (limited to 'lua_benchmark/tests/Lua-Benchmarks')
29 files changed, 1967 insertions, 0 deletions
diff --git a/lua_benchmark/tests/Lua-Benchmarks/.gitignore b/lua_benchmark/tests/Lua-Benchmarks/.gitignore new file mode 100644 index 0000000..4558236 --- /dev/null +++ b/lua_benchmark/tests/Lua-Benchmarks/.gitignore @@ -0,0 +1 @@ +results.* diff --git a/lua_benchmark/tests/Lua-Benchmarks/LICENSE b/lua_benchmark/tests/Lua-Benchmarks/LICENSE new file mode 100644 index 0000000..71c4e2e --- /dev/null +++ b/lua_benchmark/tests/Lua-Benchmarks/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2017 Gabriel de Quadros Ligneul + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/lua_benchmark/tests/Lua-Benchmarks/ack.lua b/lua_benchmark/tests/Lua-Benchmarks/ack.lua new file mode 100644 index 0000000..3b41935 --- /dev/null +++ b/lua_benchmark/tests/Lua-Benchmarks/ack.lua @@ -0,0 +1,16 @@ +-- $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)))
diff --git a/lua_benchmark/tests/Lua-Benchmarks/binary-trees.lua b/lua_benchmark/tests/Lua-Benchmarks/binary-trees.lua new file mode 100644 index 0000000..d7f52e3 --- /dev/null +++ b/lua_benchmark/tests/Lua-Benchmarks/binary-trees.lua @@ -0,0 +1,51 @@ +-- The Computer Language Benchmarks Game +-- http://benchmarksgame.alioth.debian.org/ +-- contributed by Mike Pall + +local function BottomUpTree(item, depth) + if depth > 0 then + local i = item + item + depth = depth - 1 + local left, right = BottomUpTree(i-1, depth), BottomUpTree(i, depth) + return { item, left, right } + else + return { item } + end +end + +local function ItemCheck(tree) + if tree[2] then + return tree[1] + ItemCheck(tree[2]) - ItemCheck(tree[3]) + else + return tree[1] + end +end + +-- local N = tonumber(arg and arg[1]) or 0 +local N = 15 +local mindepth = 4 +local maxdepth = mindepth + 2 +if maxdepth < N then maxdepth = N end + +do + local stretchdepth = maxdepth + 1 + local stretchtree = BottomUpTree(0, stretchdepth) + io.write(string.format("stretch tree of depth %d\t check: %d\n", + stretchdepth, ItemCheck(stretchtree))) +end + +local longlivedtree = BottomUpTree(0, maxdepth) + +for depth=mindepth,maxdepth,2 do + local iterations = 2 ^ (maxdepth - depth + mindepth) + local check = 0 + for i=1,iterations do + check = check + ItemCheck(BottomUpTree(1, depth)) + + ItemCheck(BottomUpTree(-1, depth)) + end + io.write(string.format("%d\t trees of depth %d\t check: %d\n", + iterations*2, depth, check)) +end + +io.write(string.format("long lived tree of depth %d\t check: %d\n", + maxdepth, ItemCheck(longlivedtree))) diff --git a/lua_benchmark/tests/Lua-Benchmarks/fannkuch-redux.lua b/lua_benchmark/tests/Lua-Benchmarks/fannkuch-redux.lua new file mode 100644 index 0000000..51f5fc6 --- /dev/null +++ b/lua_benchmark/tests/Lua-Benchmarks/fannkuch-redux.lua @@ -0,0 +1,49 @@ +-- The Computer Language Benchmarks Game +-- http://benchmarksgame.alioth.debian.org/ +-- contributed by Mike Pall + +local function fannkuch(n) + local p, q, s, sign, maxflips, sum = {}, {}, {}, 1, 0, 0 + for i=1,n do p[i] = i; q[i] = i; s[i] = i end + repeat + -- Copy and flip. + local q1 = p[1] -- Cache 1st element. + if q1 ~= 1 then + for i=2,n do q[i] = p[i] end -- Work on a copy. + local flips = 1 + repeat + local qq = q[q1] + if qq == 1 then -- ... until 1st element is 1. + sum = sum + sign*flips + if flips > maxflips then maxflips = flips end -- New maximum? + break + end + q[q1] = q1 + if q1 >= 4 then + local i, j = 2, q1 - 1 + repeat q[i], q[j] = q[j], q[i]; i = i + 1; j = j - 1; until i >= j + end + q1 = qq; flips = flips + 1 + until false + end + -- Permute. + if sign == 1 then + p[2], p[1] = p[1], p[2]; sign = -1 -- Rotate 1<-2. + else + p[2], p[3] = p[3], p[2]; sign = 1 -- Rotate 1<-2 and 1<-2<-3. + for i=3,n do + local sx = s[i] + if sx ~= 1 then s[i] = sx-1; break end + if i == n then return sum, maxflips end -- Out of permutations. + s[i] = i + -- Rotate 1<-...<-i+1. + local t = p[1]; for j=1,i do p[j] = p[j+1] end; p[i+1] = t + end + end + until false +end + +-- local n = tonumber(arg and arg[1]) or 7 +local n = 10 +local sum, flips = fannkuch(n) +io.write(sum, "\nPfannkuchen(", n, ") = ", flips, "\n") diff --git a/lua_benchmark/tests/Lua-Benchmarks/fasta.lua b/lua_benchmark/tests/Lua-Benchmarks/fasta.lua new file mode 100644 index 0000000..3c3fe63 --- /dev/null +++ b/lua_benchmark/tests/Lua-Benchmarks/fasta.lua @@ -0,0 +1,106 @@ +-- The Computer Language Benchmarks Game +-- http://benchmarksgame.alioth.debian.org/ +-- contributed by Mike Pall + +-- Compability with Lua 5.3 +if not loadstring then + loadstring = load +end +if not unpack then + unpack = table.unpack +end + +local Last = 42 +local function random(max) + local y = (Last * 3877 + 29573) % 139968 + Last = y + return (max * y) / 139968 +end + +local function make_repeat_fasta(id, desc, s, n) + local write, sub = io.write, string.sub + write(">", id, " ", desc, "\n") + local p, sn, s2 = 1, #s, s..s + for i=60,n,60 do + write(sub(s2, p, p + 59), "\n") + p = p + 60; if p > sn then p = p - sn end + end + local tail = n % 60 + if tail > 0 then write(sub(s2, p, p + tail-1), "\n") end +end + +local function make_random_fasta(id, desc, bs, n) + io.write(">", id, " ", desc, "\n") + loadstring([=[ + local write, char, unpack, n, random = io.write, string.char, unpack, ... + local buf, p = {}, 1 + for i=60,n,60 do + for j=p,p+59 do ]=]..bs..[=[ end + buf[p+60] = 10; p = p + 61 + if p >= 2048 then write(char(unpack(buf, 1, p-1))); p = 1 end + end + local tail = n % 60 + if tail > 0 then + for j=p,p+tail-1 do ]=]..bs..[=[ end + p = p + tail; buf[p] = 10; p = p + 1 + end + write(char(unpack(buf, 1, p-1))) + ]=], desc)(n, random) +end + +local function bisect(c, p, lo, hi) + local n = hi - lo + if n == 0 then return "buf[j] = "..c[hi].."\n" end + local mid = math.floor(n / 2) + return "if r < "..p[lo+mid].." then\n"..bisect(c, p, lo, lo+mid).. + "else\n"..bisect(c, p, lo+mid+1, hi).."end\n" +end + +local function make_bisect(tab) + local c, p, sum = {}, {}, 0 + for i,row in ipairs(tab) do + c[i] = string.byte(row[1]) + sum = sum + row[2] + p[i] = sum + end + return "local r = random(1)\n"..bisect(c, p, 1, #tab) +end + +local alu = + "GGCCGGGCGCGGTGGCTCACGCCTGTAATCCCAGCACTTTGG".. + "GAGGCCGAGGCGGGCGGATCACCTGAGGTCAGGAGTTCGAGA".. + "CCAGCCTGGCCAACATGGTGAAACCCCGTCTCTACTAAAAAT".. + "ACAAAAATTAGCCGGGCGTGGTGGCGCGCGCCTGTAATCCCA".. + "GCTACTCGGGAGGCTGAGGCAGGAGAATCGCTTGAACCCGGG".. + "AGGCGGAGGTTGCAGTGAGCCGAGATCGCGCCACTGCACTCC".. + "AGCCTGGGCGACAGAGCGAGACTCCGTCTCAAAAA" + +local iub = make_bisect{ + { "a", 0.27 }, + { "c", 0.12 }, + { "g", 0.12 }, + { "t", 0.27 }, + { "B", 0.02 }, + { "D", 0.02 }, + { "H", 0.02 }, + { "K", 0.02 }, + { "M", 0.02 }, + { "N", 0.02 }, + { "R", 0.02 }, + { "S", 0.02 }, + { "V", 0.02 }, + { "W", 0.02 }, + { "Y", 0.02 }, +} + +local homosapiens = make_bisect{ + { "a", 0.3029549426680 }, + { "c", 0.1979883004921 }, + { "g", 0.1975473066391 }, + { "t", 0.3015094502008 }, +} + +local N = tonumber(arg and arg[1]) or 1000 +make_repeat_fasta('ONE', 'Homo sapiens alu', alu, N*2) +make_random_fasta('TWO', 'IUB ambiguity codes', iub, N*3) +make_random_fasta('THREE', 'Homo sapiens frequency', homosapiens, N*5) diff --git a/lua_benchmark/tests/Lua-Benchmarks/fixpoint-fact.lua b/lua_benchmark/tests/Lua-Benchmarks/fixpoint-fact.lua new file mode 100644 index 0000000..743a67c --- /dev/null +++ b/lua_benchmark/tests/Lua-Benchmarks/fixpoint-fact.lua @@ -0,0 +1,24 @@ +-- fixed-point operator
+local Z = function (le)
+ local a = function (f)
+ return le(function (x) return f(f)(x) end)
+ end
+ return a(a)
+ end
+
+
+-- non-recursive factorial
+
+local F = function (f)
+ return function (n)
+ if n == 0 then return 1
+ else return n*f(n-1) end
+ end
+ end
+
+local fat = Z(F)
+
+local s = 0
+for i = 1, arg[1] or 100 do s = s + fat(i) end
+print(s)
+
diff --git a/lua_benchmark/tests/Lua-Benchmarks/heapsort.lua b/lua_benchmark/tests/Lua-Benchmarks/heapsort.lua new file mode 100644 index 0000000..0aaf7cb --- /dev/null +++ b/lua_benchmark/tests/Lua-Benchmarks/heapsort.lua @@ -0,0 +1,48 @@ +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
+
diff --git a/lua_benchmark/tests/Lua-Benchmarks/k-nucleotide.lua b/lua_benchmark/tests/Lua-Benchmarks/k-nucleotide.lua new file mode 100644 index 0000000..5deb58d --- /dev/null +++ b/lua_benchmark/tests/Lua-Benchmarks/k-nucleotide.lua @@ -0,0 +1,66 @@ +-- The Computer Language Benchmarks Game +-- http://benchmarksgame.alioth.debian.org/ +-- contributed by Mike Pall + +local function kfrequency(seq, freq, k, frame) + local sub = string.sub + local k1 = k - 1 + for i=frame,string.len(seq)-k1,k do + local c = sub(seq, i, i+k1) + freq[c] = freq[c] + 1 + end +end + +local function freqdefault() + return 0 +end + +local function count(seq, frag) + local k = string.len(frag) + local freq = setmetatable({}, { __index = freqdefault }) + for frame=1,k do kfrequency(seq, freq, k, frame) end + io.write(freq[frag] or 0, "\t", frag, "\n") +end + +local function frequency(seq, k) + local freq = setmetatable({}, { __index = freqdefault }) + for frame=1,k do kfrequency(seq, freq, k, frame) end + local sfreq, sn = {}, 1 + for c,v in pairs(freq) do sfreq[sn] = c; sn = sn + 1 end + table.sort(sfreq, function(a, b) + local fa, fb = freq[a], freq[b] + return fa == fb and a > b or fa > fb + end) + sum = string.len(seq)-k+1 + for _,c in ipairs(sfreq) do + io.write(string.format("%s %0.3f\n", c, (freq[c]*100)/sum)) + end + io.write("\n") +end + +local function readseq() + local sub = string.sub + for line in io.lines() do + if sub(line, 1, 1) == ">" and sub(line, 2, 6) == "THREE" then break end + end + local lines, ln = {}, 0 + for line in io.lines() do + local c = sub(line, 1, 1) + if c == ">" then + break + elseif c ~= ";" then + ln = ln + 1 + lines[ln] = line + end + end + return string.upper(table.concat(lines, "", 1, ln)) +end + +local seq = readseq() +frequency(seq, 1) +frequency(seq, 2) +count(seq, "GGT") +count(seq, "GGTA") +count(seq, "GGTATT") +count(seq, "GGTATTTTAATT") +count(seq, "GGTATTTTAATTTATAGT") 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)
diff --git a/lua_benchmark/tests/Lua-Benchmarks/n-body.lua b/lua_benchmark/tests/Lua-Benchmarks/n-body.lua new file mode 100644 index 0000000..40cee46 --- /dev/null +++ b/lua_benchmark/tests/Lua-Benchmarks/n-body.lua @@ -0,0 +1,121 @@ +-- The Computer Language Benchmarks Game +-- http://benchmarksgame.alioth.debian.org/ +-- contributed by Mike Pall +-- modified by Geoff Leyland +-- modified by Mario Pernici + +sun = {} +jupiter = {} +saturn = {} +uranus = {} +neptune = {} + +local sqrt = math.sqrt + +local PI = 3.141592653589793 +local SOLAR_MASS = 4 * PI * PI +local DAYS_PER_YEAR = 365.24 +sun.x = 0.0 +sun.y = 0.0 +sun.z = 0.0 +sun.vx = 0.0 +sun.vy = 0.0 +sun.vz = 0.0 +sun.mass = SOLAR_MASS +jupiter.x = 4.84143144246472090e+00 +jupiter.y = -1.16032004402742839e+00 +jupiter.z = -1.03622044471123109e-01 +jupiter.vx = 1.66007664274403694e-03 * DAYS_PER_YEAR +jupiter.vy = 7.69901118419740425e-03 * DAYS_PER_YEAR +jupiter.vz = -6.90460016972063023e-05 * DAYS_PER_YEAR +jupiter.mass = 9.54791938424326609e-04 * SOLAR_MASS +saturn.x = 8.34336671824457987e+00 +saturn.y = 4.12479856412430479e+00 +saturn.z = -4.03523417114321381e-01 +saturn.vx = -2.76742510726862411e-03 * DAYS_PER_YEAR +saturn.vy = 4.99852801234917238e-03 * DAYS_PER_YEAR +saturn.vz = 2.30417297573763929e-05 * DAYS_PER_YEAR +saturn.mass = 2.85885980666130812e-04 * SOLAR_MASS +uranus.x = 1.28943695621391310e+01 +uranus.y = -1.51111514016986312e+01 +uranus.z = -2.23307578892655734e-01 +uranus.vx = 2.96460137564761618e-03 * DAYS_PER_YEAR +uranus.vy = 2.37847173959480950e-03 * DAYS_PER_YEAR +uranus.vz = -2.96589568540237556e-05 * DAYS_PER_YEAR +uranus.mass = 4.36624404335156298e-05 * SOLAR_MASS +neptune.x = 1.53796971148509165e+01 +neptune.y = -2.59193146099879641e+01 +neptune.z = 1.79258772950371181e-01 +neptune.vx = 2.68067772490389322e-03 * DAYS_PER_YEAR +neptune.vy = 1.62824170038242295e-03 * DAYS_PER_YEAR +neptune.vz = -9.51592254519715870e-05 * DAYS_PER_YEAR +neptune.mass = 5.15138902046611451e-05 * SOLAR_MASS + +local bodies = {sun,jupiter,saturn,uranus,neptune} + +local function advance(bodies, nbody, dt) + for i=1,nbody do + local bi = bodies[i] + local bix, biy, biz, bimass = bi.x, bi.y, bi.z, bi.mass + local bivx, bivy, bivz = bi.vx, bi.vy, bi.vz + for j=i+1,nbody do + local bj = bodies[j] + local dx, dy, dz = bix-bj.x, biy-bj.y, biz-bj.z + local dist2 = dx*dx + dy*dy + dz*dz + local mag = sqrt(dist2) + mag = dt / (mag * dist2) + local bm = bj.mass*mag + bivx = bivx - (dx * bm) + bivy = bivy - (dy * bm) + bivz = bivz - (dz * bm) + bm = bimass*mag + bj.vx = bj.vx + (dx * bm) + bj.vy = bj.vy + (dy * bm) + bj.vz = bj.vz + (dz * bm) + end + bi.vx = bivx + bi.vy = bivy + bi.vz = bivz + bi.x = bix + dt * bivx + bi.y = biy + dt * bivy + bi.z = biz + dt * bivz + end +end + +local function energy(bodies, nbody) + local e = 0 + for i=1,nbody do + local bi = bodies[i] + local vx, vy, vz, bim = bi.vx, bi.vy, bi.vz, bi.mass + e = e + (0.5 * bim * (vx*vx + vy*vy + vz*vz)) + for j=i+1,nbody do + local bj = bodies[j] + local dx, dy, dz = bi.x-bj.x, bi.y-bj.y, bi.z-bj.z + local distance = sqrt(dx*dx + dy*dy + dz*dz) + e = e - ((bim * bj.mass) / distance) + end + end + return e +end + +local function offsetMomentum(b, nbody) + local px, py, pz = 0, 0, 0 + for i=1,nbody do + local bi = b[i] + local bim = bi.mass + px = px + (bi.vx * bim) + py = py + (bi.vy * bim) + pz = pz + (bi.vz * bim) + end + b[1].vx = -px / SOLAR_MASS + b[1].vy = -py / SOLAR_MASS + b[1].vz = -pz / SOLAR_MASS +end + +local N = tonumber(arg and arg[1]) or 1000 +local nbody = #bodies + +offsetMomentum(bodies, nbody) +io.write( string.format("%0.9f",energy(bodies, nbody)), "\n") +for i=1,N do advance(bodies, nbody, 0.01) end +io.write( string.format("%0.9f",energy(bodies, nbody)), "\n") diff --git a/lua_benchmark/tests/Lua-Benchmarks/plot.gpi b/lua_benchmark/tests/Lua-Benchmarks/plot.gpi new file mode 100644 index 0000000..24277f4 --- /dev/null +++ b/lua_benchmark/tests/Lua-Benchmarks/plot.gpi @@ -0,0 +1,18 @@ +set encoding utf8 +set terminal png size 3000,1000 font 'Helvetica,22' +set output outfile + +set style data histogram +set style histogram cluster gap 1 +set style fill solid border -1 +set xtic font "Helvetica,14" +set boxwidth 0.9 +set grid +set key bmargin center horizontal Left reverse noenhanced autotitle columnhead nobox +set ylabel ylabel + +plot for[i=2:1 + nbinaries] datafile \ + using i:xtic(1) \ + title column \ + ls i + diff --git a/lua_benchmark/tests/Lua-Benchmarks/qt.lua b/lua_benchmark/tests/Lua-Benchmarks/qt.lua new file mode 100644 index 0000000..f1de543 --- /dev/null +++ b/lua_benchmark/tests/Lua-Benchmarks/qt.lua @@ -0,0 +1,304 @@ +-- qt.lua,15 (one edge vector)
+-- Julia sets via interval cell-mapping (quadtree version)
+
+--require"julia" local f=f
+
+local io=io
+local root,exterior
+local cx,cy
+local Rxmin,Rxmax,Rymin,Rymax=-2.0,2.0,-2.0,2.0
+local white=1.0
+local black=0.0
+local gray=0.5
+local N=0
+local nE=0
+local E={}
+local write=io.write
+
+local function output(a1,a2,a3,a4,a5,a6)
+ write(
+ a1 or ""," ",
+ a2 or ""," ",
+ a3 or ""," ",
+ a4 or ""," ",
+ a5 or ""," ",
+ a6 or ""," \n")
+end
+
+local function imul(xmin,xmax,ymin,ymax)
+ local mm=xmin*ymin
+ local mM=xmin*ymax
+ local Mm=xmax*ymin
+ local MM=xmax*ymax
+ local m,M=mm,mm
+ if m>mM then m=mM elseif M<mM then M=mM end
+ if m>Mm then m=Mm elseif M<Mm then M=Mm end
+ if m>MM then m=MM elseif M<MM then M=MM end
+ return m,M
+end
+
+local function isqr(xmin,xmax)
+ local u=xmin*xmin
+ local v=xmax*xmax
+ if xmin<=0.0 and 0.0<=xmax then
+ if u<v then return 0.0,v else return 0.0,u end
+ else
+ if u<v then return u,v else return v,u end
+ end
+end
+
+local function f(xmin,xmax,ymin,ymax)
+ local x2min,x2max=isqr(xmin,xmax)
+ local y2min,y2max=isqr(ymin,ymax)
+ local xymin,xymax=imul(xmin,xmax,ymin,ymax)
+ return x2min-y2max+cx,x2max-y2min+cx,2.0*xymin+cy,2.0*xymax+cy
+end
+
+local function outside(xmin,xmax,ymin,ymax)
+ local x,y
+ if 0.0<xmin then x=xmin elseif 0.0<xmax then x=0.0 else x=xmax end
+ if 0.0<ymin then y=ymin elseif 0.0<ymax then y=0.0 else y=ymax end
+ return x^2+y^2>4.0
+end
+
+local function inside(xmin,xmax,ymin,ymax)
+ return xmin^2+ymin^2<=4.0 and xmin^2+ymax^2<=4.0 and
+ xmax^2+ymin^2<=4.0 and xmax^2+ymax^2<=4.0
+end
+
+local function newcell()
+ return {nil,nil,nil,nil,color=gray}
+end
+
+local function addedge(a,b)
+ nE=nE+1
+ E[nE]=b
+end
+
+local function refine(q)
+ if q.color==gray then
+ if q[1]==nil then
+ q[1]=newcell()
+ q[2]=newcell()
+ q[3]=newcell()
+ q[4]=newcell()
+ else
+ refine(q[1])
+ refine(q[2])
+ refine(q[3])
+ refine(q[4])
+ end
+ end
+end
+
+local function clip(q,xmin,xmax,ymin,ymax,o,oxmin,oxmax,oymin,oymax)
+ local ixmin,ixmax,iymin,iymax
+ if xmin>oxmin then ixmin=xmin else ixmin=oxmin end
+ if xmax<oxmax then ixmax=xmax else ixmax=oxmax end
+ if ixmin>=ixmax then return end
+ if ymin>oymin then iymin=ymin else iymin=oymin end
+ if ymax<oymax then iymax=ymax else iymax=oymax end
+ --if ixmin<=ixmax and iymin<=iymax then
+ if iymin<iymax then
+ if q[1]==nil then
+ addedge(o,q)
+ else
+ local xmid=(xmin+xmax)/2.0
+ local ymid=(ymin+ymax)/2.0
+ clip(q[1],xmin,xmid,ymid,ymax,o,oxmin,oxmax,oymin,oymax)
+ clip(q[2],xmid,xmax,ymid,ymax,o,oxmin,oxmax,oymin,oymax)
+ clip(q[3],xmin,xmid,ymin,ymid,o,oxmin,oxmax,oymin,oymax)
+ clip(q[4],xmid,xmax,ymin,ymid,o,oxmin,oxmax,oymin,oymax)
+ end
+ end
+end
+
+local function map(q,xmin,xmax,ymin,ymax)
+ --xmin,xmax,ymin,ymax=f(xmin,xmax,ymin,ymax,cx,cy)
+ xmin,xmax,ymin,ymax=f(xmin,xmax,ymin,ymax)
+ if outside(xmin,xmax,ymin,ymax) then
+ q.color=white
+ else
+ if not inside(xmin,xmax,ymin,ymax) then addedge(q,exterior) end
+ clip(root,Rxmin,Rxmax,Rymin,Rymax,q,xmin,xmax,ymin,ymax)
+ end
+end
+
+local function update(q,xmin,xmax,ymin,ymax)
+ if q.color==gray then
+ if q[1]==nil then
+ local b=nE
+ q[2]=nE+1
+ map(q,xmin,xmax,ymin,ymax)
+ q[3]=nE
+ else
+ local xmid=(xmin+xmax)/2.0
+ local ymid=(ymin+ymax)/2.0
+ update(q[1],xmin,xmid,ymid,ymax)
+ update(q[2],xmid,xmax,ymid,ymax)
+ update(q[3],xmin,xmid,ymin,ymid)
+ update(q[4],xmid,xmax,ymin,ymid)
+ end
+ end
+end
+
+local function color(q)
+ if q.color==gray then
+ if q[1]==nil then
+ for i=q[2],q[3] do
+ if E[i].color~=white then return end
+ end
+ q.color=white N=N+1
+ else
+ color(q[1])
+ color(q[2])
+ color(q[3])
+ color(q[4])
+ end
+ end
+end
+
+local function prewhite(q)
+ if q.color==gray then
+ if q[1]==nil then
+ for i=q[2],q[3] do
+ local c=E[i].color
+ if c==white or c==-gray then
+ q.color=-gray
+ N=N+1
+ return
+ end
+ end
+ else
+ prewhite(q[1])
+ prewhite(q[2])
+ prewhite(q[3])
+ prewhite(q[4])
+ end
+ end
+end
+
+local function recolor(q)
+ if q.color==-gray then
+ q.color=gray
+ elseif q.color==gray then
+ if q[1]==nil then
+ q.color=black
+ else
+ recolor(q[1])
+ recolor(q[2])
+ recolor(q[3])
+ recolor(q[4])
+ end
+ end
+end
+
+local function area(q)
+ if q[1]==nil then
+ if q.color==white then return 0.0,0.0
+ elseif q.color==black then return 0.0,1.0
+ else return 1.0,0.0 end
+ else
+ local g1,b1=area(q[1])
+ local g2,b2=area(q[2])
+ local g3,b3=area(q[3])
+ local g4,b4=area(q[4])
+ return (g1+g2+g3+g4)/4.0, (b1+b2+b3+b4)/4.0
+ end
+end
+
+local function colorup(q)
+ if q[1]~=nil and q.color==gray then
+ local c1=colorup(q[1])
+ local c2=colorup(q[2])
+ local c3=colorup(q[3])
+ local c4=colorup(q[4])
+ if c1==c2 and c1==c3 and c1==c4 then
+if c1~=gray then
+ q[1]=nil; --q[2]=nil; q[3]=nil; q[4]=nil
+N=N+1 end
+ q.color=c1
+ end
+ end
+ return q.color
+end
+
+local function save(q,xmin,ymin,N)
+ if q[1]==nil or N==1 then
+ output(xmin,ymin,N,q.color)
+ else
+ N=N/2
+ local xmid=xmin+N
+ local ymid=ymin+N
+ save(q[1],xmin,ymin,N)
+ save(q[2],xmid,ymin,N)
+ save(q[3],xmin,ymid,N)
+ save(q[4],xmid,ymid,N)
+ end
+end
+local function show(p)
+ local N=2^10
+ -- io.output(p..".box")
+ output(N)
+ save(root,0,0,N)
+ -- io.close()
+end
+
+local t0=0
+local function memory(s)
+ local t=os.clock()
+ --local dt=string.format("%f",t-t0)
+ local dt=t-t0
+ io.stdout:write(s,"\t",dt," sec\t",t," sec\t",math.floor(collectgarbage"count"/1024),"M\n")
+ t0=t
+end
+
+local function do_(f,s)
+ local a,b=f(root,Rxmin,Rxmax,Rymin,Rymax)
+ memory(s)
+ return a,b
+end
+
+local function julia(l,a,b)
+memory"begin"
+ cx=a cy=b
+ root=newcell()
+ exterior=newcell() exterior.color=white
+ show(0)
+ for i=1,l do print("\nstep",i)
+ nE=0
+ do_(refine,"refine")
+ do_(update,"update")
+ repeat
+ N=0 color(root,Rxmin,Rxmax,Rymin,Rymax) print("color",N)
+ until N==0 memory"color"
+ repeat
+ N=0 prewhite(root,Rxmin,Rxmax,Rymin,Rymax) print("prewhite",N)
+ until N==0 memory"prewhite"
+ do_(recolor,"recolor")
+ do_(colorup,"colorup") print("colorup",N)
+ local g,b=do_(area,"area") print("area",g,b,g+b)
+ show(i) memory"output"
+ print("edges",nE)
+ end
+end
+
+--julia(14,0.25,0.35)
+--julia(14, -.12, .74 )
+--julia(14,0,0)
+--julia(12,0.25,0)
+--julia(9,0.24,0)
+--julia(9,0.26,0)
+--julia(13,0,1)
+--julia(12,-1.1,0)
+--julia(9,-0.12,0.5)
+
+-- figures for paper
+--julia(14,0,1)
+--julia(14,-1,0)
+--julia(12,-0.12, 0.64)
+--julia(14,-0.12, 0.60)
+--julia(14,-0.12, 0.30)
+
+-- julia (level, a, b) -- julia set de c= a + b i
+julia(10,-0.25, 0.74)
diff --git a/lua_benchmark/tests/Lua-Benchmarks/queen.lua b/lua_benchmark/tests/Lua-Benchmarks/queen.lua new file mode 100644 index 0000000..9071406 --- /dev/null +++ b/lua_benchmark/tests/Lua-Benchmarks/queen.lua @@ -0,0 +1,46 @@ +local N = tonumber(arg[1] or 8) -- board size
+
+
+-- check whether position (n,c) is free from attacks
+local function isplaceok (a, n, c)
+ for i = 1, n - 1 do -- for each queen already placed
+ if (a[i] == c) or -- same column?
+ (a[i] - i == c - n) or -- same diagonal?
+ (a[i] + i == c + n) then -- same diagonal?
+ return false -- place can be attacked
+ end
+ end
+ return true -- no attacks; place is OK
+end
+
+
+-- print a board
+local function printsolution (a)
+ for i = 1, N do
+ for j = 1, N do
+ io.write(a[i] == j and "X" or "-", " ")
+ end
+ io.write("\n")
+ end
+ io.write("\n")
+end
+
+
+-- add to board 'a' all queens from 'n' to 'N'
+local function addqueen (a, n)
+ if n > N then -- all queens have been placed?
+ printsolution(a)
+ else -- try to place n-th queen
+ for c = 1, N do
+ if isplaceok(a, n, c) then
+ a[n] = c -- place n-th queen at column 'c'
+ addqueen(a, n + 1)
+ end
+ end
+ end
+end
+
+
+-- run the program
+addqueen({}, 1)
+
diff --git a/lua_benchmark/tests/Lua-Benchmarks/readme.md b/lua_benchmark/tests/Lua-Benchmarks/readme.md new file mode 100644 index 0000000..b1b6624 --- /dev/null +++ b/lua_benchmark/tests/Lua-Benchmarks/readme.md @@ -0,0 +1,31 @@ +# Lua Benchmarks + +Compare the performance of different Lua implementations + +## Sample Results + +Computer information: + +``` +Distro: CentOS release 6.8 (Final) +Kernel: 2.6.32-642.13.1.el6.x86_64 +CPU: Intel(R) Core(TM) i7-4770 CPU @ 3.40GHz +``` +![](https://raw.githubusercontent.com/gligneul/Lua-Benchmarks/master/results/speedup_luajit.png) +![](https://raw.githubusercontent.com/gligneul/Lua-Benchmarks/master/results/speedup_lua53.png) +![](https://raw.githubusercontent.com/gligneul/Lua-Benchmarks/master/results/speedup_lua5.png) + +## Usage + +``` +usage: lua runbenchmarks.lua [options] +options: + --nruns <n> number of times that each test is executed (default = 3) + --no-supress don't supress error messages from tests + --output <name> name of the benchmark output + --normalize normalize the result based on the first binary + --speedup compute the speedup based on the first binary + --no-plot don't create the plot with gnuplot + --help show this message +``` + diff --git a/lua_benchmark/tests/Lua-Benchmarks/regex-dna.lua b/lua_benchmark/tests/Lua-Benchmarks/regex-dna.lua new file mode 100644 index 0000000..2627e05 --- /dev/null +++ b/lua_benchmark/tests/Lua-Benchmarks/regex-dna.lua @@ -0,0 +1,59 @@ +-- The Computer Language Benchmarks Game +-- http://benchmarksgame.alioth.debian.org/ +-- contributed by Jim Roseborough +-- modified by Victor Tang +-- optimized & replaced inefficient use of gsub with gmatch +-- partitioned sequence to prevent extraneous redundant string copy + +seq = io.read("*a") +ilen, seq = #seq, seq:gsub('>[^%c]*%c*', ''):gsub('%c+', '') +clen = #seq + +local variants = { 'agggtaaa|tttaccct', + '[cgt]gggtaaa|tttaccc[acg]', + 'a[act]ggtaaa|tttacc[agt]t', + 'ag[act]gtaaa|tttac[agt]ct', + 'agg[act]taaa|ttta[agt]cct', + 'aggg[acg]aaa|ttt[cgt]ccct', + 'agggt[cgt]aa|tt[acg]accct', + 'agggta[cgt]a|t[acg]taccct', + 'agggtaa[cgt]|[acg]ttaccct', } + +local subst = { B='(c|g|t)', D='(a|g|t)', H='(a|c|t)', K='(g|t)', + M='(a|c)', N='(a|c|g|t)', R='(a|g)', S='(c|g)', + V='(a|c|g)', W='(a|t)', Y='(c|t)' } + +function countmatches(variant) + local n = 0 + variant:gsub('([^|]+)|?', function(pattern) + for _ in seq:gmatch(pattern) do n = n + 1 end + end) + return n +end + +for _, p in ipairs(variants) do + io.write( string.format('%s %d\n', p, countmatches(p)) ) +end + +function partitionstring(seq) + local seg = math.floor( math.sqrt(#seq) ) + local seqtable = {} + for nextstart = 1, #seq, seg do + table.insert(seqtable, seq:sub(nextstart, nextstart + seg - 1)) + end + return seqtable +end +function chunk_gsub(t, k, v) + for i, p in ipairs(t) do + t[i] = p:find(k) and p:gsub(k, v) or t[i] + end + return t +end + +seq = partitionstring(seq) +for k, v in pairs(subst) do + chunk_gsub(seq, k, v) +end +seq = table.concat(seq) +io.write(string.format('\n%d\n%d\n%d\n', ilen, clen, #seq)) + diff --git a/lua_benchmark/tests/Lua-Benchmarks/results/speedup_lua5.png b/lua_benchmark/tests/Lua-Benchmarks/results/speedup_lua5.png Binary files differnew file mode 100644 index 0000000..3a96c4b --- /dev/null +++ b/lua_benchmark/tests/Lua-Benchmarks/results/speedup_lua5.png diff --git a/lua_benchmark/tests/Lua-Benchmarks/results/speedup_lua5.txt b/lua_benchmark/tests/Lua-Benchmarks/results/speedup_lua5.txt new file mode 100644 index 0000000..4229164 --- /dev/null +++ b/lua_benchmark/tests/Lua-Benchmarks/results/speedup_lua5.txt @@ -0,0 +1,14 @@ +test lua-5.1.5 lua-5.2.4 lua-5.3.4 +ack 1.0 1.0106382978723 1.0249494268375 +fixpoint-fact 1.0 0.87217724755006 0.93172507965407 +heapsort 1.0 0.67983128834356 1.0497335701599 +mandelbrot 1.0 0.9497287522604 1.1796945193172 +juliaset 1.0 0.90391321193336 1.0648105887723 +queen 1.0 0.82105263157895 1.0067934782609 +sieve 1.0 0.93239227340267 1.2178554099951 +binary 1.0 1.9514442231076 1.3708238586671 +n-body 1.0 0.90884932234919 1.2101910828025 +fannkuch 1.0 0.80147058823529 1.1354166666667 +fasta 1.0 0.93725490196078 0.91483253588517 +k-nucleotide 1.0 0.95159059474412 1.0747201249675 +spectral-norm 1.0 0.93865698729583 0.97695504344541 diff --git a/lua_benchmark/tests/Lua-Benchmarks/results/speedup_lua53.png b/lua_benchmark/tests/Lua-Benchmarks/results/speedup_lua53.png Binary files differnew file mode 100644 index 0000000..a701896 --- /dev/null +++ b/lua_benchmark/tests/Lua-Benchmarks/results/speedup_lua53.png diff --git a/lua_benchmark/tests/Lua-Benchmarks/results/speedup_lua53.txt b/lua_benchmark/tests/Lua-Benchmarks/results/speedup_lua53.txt new file mode 100644 index 0000000..162511f --- /dev/null +++ b/lua_benchmark/tests/Lua-Benchmarks/results/speedup_lua53.txt @@ -0,0 +1,14 @@ +test lua-5.3.0 lua-5.3.1 lua-5.3.2 lua-5.3.3 lua-5.3.4 +ack 1.0 1.0047169811321 1.0142857142857 1.0205338809035 1.0060728744939 +fixpoint-fact 1.0 0.98553429771349 0.98050139275766 0.94496644295302 0.94538943598926 +heapsort 1.0 0.93150999770168 1.1819772528434 1.2005331753555 1.1812882541533 +mandelbrot 1.0 0.96816037735849 1.1200545702592 1.1439851370181 1.1000446627959 +juliaset 1.0 0.95912698412698 1.0563811188811 1.0431592576608 1.1247091670544 +queen 1.0 0.96021908330931 1.1372482075794 1.1411442274752 1.1318382602786 +sieve 1.0 0.98799039231385 1.1808612440191 1.1963160445952 1.1836930455635 +binary 1.0 1.0183116645303 1.011458712259 1.0568225009502 1.0275314116778 +n-body 1.0 0.96153846153846 1.1900714042843 1.2608069164265 1.2169680111266 +fannkuch 1.0 0.96326981496824 1.2090121317158 1.2183024799162 1.2106907323846 +fasta 1.0 0.96836313617607 1.0004737091426 1.0095602294455 1.0114942528736 +k-nucleotide 1.0 1.0283306017326 1.0588235294118 1.1467362924282 1.1683958499601 +spectral-norm 1.0 0.98923646148671 1.1707802547771 1.1372776488786 1.1320246343341 diff --git a/lua_benchmark/tests/Lua-Benchmarks/results/speedup_lua53_log.txt b/lua_benchmark/tests/Lua-Benchmarks/results/speedup_lua53_log.txt new file mode 100644 index 0000000..e2ff8a4 --- /dev/null +++ b/lua_benchmark/tests/Lua-Benchmarks/results/speedup_lua53_log.txt @@ -0,0 +1,69 @@ +Distro: CentOS release 6.8 (Final) +Kernel: 2.6.32-642.13.1.el6.x86_64 +CPU: Intel(R) Core(TM) i7-4770 CPU @ 3.40GHz +running "lua-5.3.0 ./ack.lua 3 10"... done +running "lua-5.3.1 ./ack.lua 3 10"... done +running "lua-5.3.2 ./ack.lua 3 10"... done +running "lua-5.3.3 ./ack.lua 3 10"... done +running "lua ./ack.lua 3 10"... done +running "lua-5.3.0 ./fixpoint-fact.lua 3000"... done +running "lua-5.3.1 ./fixpoint-fact.lua 3000"... done +running "lua-5.3.2 ./fixpoint-fact.lua 3000"... done +running "lua-5.3.3 ./fixpoint-fact.lua 3000"... done +running "lua ./fixpoint-fact.lua 3000"... done +running "lua-5.3.0 ./heapsort.lua 10 250000"... done +running "lua-5.3.1 ./heapsort.lua 10 250000"... done +running "lua-5.3.2 ./heapsort.lua 10 250000"... done +running "lua-5.3.3 ./heapsort.lua 10 250000"... done +running "lua ./heapsort.lua 10 250000"... done +running "lua-5.3.0 ./mandel.lua"... done +running "lua-5.3.1 ./mandel.lua"... done +running "lua-5.3.2 ./mandel.lua"... done +running "lua-5.3.3 ./mandel.lua"... done +running "lua ./mandel.lua"... done +running "lua-5.3.0 ./qt.lua"... done +running "lua-5.3.1 ./qt.lua"... done +running "lua-5.3.2 ./qt.lua"... done +running "lua-5.3.3 ./qt.lua"... done +running "lua ./qt.lua"... done +running "lua-5.3.0 ./queen.lua 12"... done +running "lua-5.3.1 ./queen.lua 12"... done +running "lua-5.3.2 ./queen.lua 12"... done +running "lua-5.3.3 ./queen.lua 12"... done +running "lua ./queen.lua 12"... done +running "lua-5.3.0 ./sieve.lua 5000"... done +running "lua-5.3.1 ./sieve.lua 5000"... done +running "lua-5.3.2 ./sieve.lua 5000"... done +running "lua-5.3.3 ./sieve.lua 5000"... done +running "lua ./sieve.lua 5000"... done +running "lua-5.3.0 ./binary-trees.lua 15"... done +running "lua-5.3.1 ./binary-trees.lua 15"... done +running "lua-5.3.2 ./binary-trees.lua 15"... done +running "lua-5.3.3 ./binary-trees.lua 15"... done +running "lua ./binary-trees.lua 15"... done +running "lua-5.3.0 ./n-body.lua 1000000"... done +running "lua-5.3.1 ./n-body.lua 1000000"... done +running "lua-5.3.2 ./n-body.lua 1000000"... done +running "lua-5.3.3 ./n-body.lua 1000000"... done +running "lua ./n-body.lua 1000000"... done +running "lua-5.3.0 ./fannkuch-redux.lua 10"... done +running "lua-5.3.1 ./fannkuch-redux.lua 10"... done +running "lua-5.3.2 ./fannkuch-redux.lua 10"... done +running "lua-5.3.3 ./fannkuch-redux.lua 10"... done +running "lua ./fannkuch-redux.lua 10"... done +running "lua-5.3.0 ./fasta.lua 2500000"... done +running "lua-5.3.1 ./fasta.lua 2500000"... done +running "lua-5.3.2 ./fasta.lua 2500000"... done +running "lua-5.3.3 ./fasta.lua 2500000"... done +running "lua ./fasta.lua 2500000"... done +running "lua-5.3.0 ./k-nucleotide.lua < fasta1000000.txt"... done +running "lua-5.3.1 ./k-nucleotide.lua < fasta1000000.txt"... done +running "lua-5.3.2 ./k-nucleotide.lua < fasta1000000.txt"... done +running "lua-5.3.3 ./k-nucleotide.lua < fasta1000000.txt"... done +running "lua ./k-nucleotide.lua < fasta1000000.txt"... done +running "lua-5.3.0 ./spectral-norm.lua 1000"... done +running "lua-5.3.1 ./spectral-norm.lua 1000"... done +running "lua-5.3.2 ./spectral-norm.lua 1000"... done +running "lua-5.3.3 ./spectral-norm.lua 1000"... done +running "lua ./spectral-norm.lua 1000"... done +final done diff --git a/lua_benchmark/tests/Lua-Benchmarks/results/speedup_lua5_log.txt b/lua_benchmark/tests/Lua-Benchmarks/results/speedup_lua5_log.txt new file mode 100644 index 0000000..96d8a9f --- /dev/null +++ b/lua_benchmark/tests/Lua-Benchmarks/results/speedup_lua5_log.txt @@ -0,0 +1,43 @@ +Distro: CentOS release 6.8 (Final) +Kernel: 2.6.32-642.13.1.el6.x86_64 +CPU: Intel(R) Core(TM) i7-4770 CPU @ 3.40GHz +running "lua-5.1.5 ./ack.lua 3 10"... done +running "lua-5.2.4 ./ack.lua 3 10"... done +running "lua ./ack.lua 3 10"... done +running "lua-5.1.5 ./fixpoint-fact.lua 3000"... done +running "lua-5.2.4 ./fixpoint-fact.lua 3000"... done +running "lua ./fixpoint-fact.lua 3000"... done +running "lua-5.1.5 ./heapsort.lua 10 250000"... done +running "lua-5.2.4 ./heapsort.lua 10 250000"... done +running "lua ./heapsort.lua 10 250000"... done +running "lua-5.1.5 ./mandel.lua"... done +running "lua-5.2.4 ./mandel.lua"... done +running "lua ./mandel.lua"... done +running "lua-5.1.5 ./qt.lua"... done +running "lua-5.2.4 ./qt.lua"... done +running "lua ./qt.lua"... done +running "lua-5.1.5 ./queen.lua 12"... done +running "lua-5.2.4 ./queen.lua 12"... done +running "lua ./queen.lua 12"... done +running "lua-5.1.5 ./sieve.lua 5000"... done +running "lua-5.2.4 ./sieve.lua 5000"... done +running "lua ./sieve.lua 5000"... done +running "lua-5.1.5 ./binary-trees.lua 15"... done +running "lua-5.2.4 ./binary-trees.lua 15"... done +running "lua ./binary-trees.lua 15"... done +running "lua-5.1.5 ./n-body.lua 1000000"... done +running "lua-5.2.4 ./n-body.lua 1000000"... done +running "lua ./n-body.lua 1000000"... done +running "lua-5.1.5 ./fannkuch-redux.lua 10"... done +running "lua-5.2.4 ./fannkuch-redux.lua 10"... done +running "lua ./fannkuch-redux.lua 10"... done +running "lua-5.1.5 ./fasta.lua 2500000"... done +running "lua-5.2.4 ./fasta.lua 2500000"... done +running "lua ./fasta.lua 2500000"... done +running "lua-5.1.5 ./k-nucleotide.lua < fasta1000000.txt"... done +running "lua-5.2.4 ./k-nucleotide.lua < fasta1000000.txt"... done +running "lua ./k-nucleotide.lua < fasta1000000.txt"... done +running "lua-5.1.5 ./spectral-norm.lua 1000"... done +running "lua-5.2.4 ./spectral-norm.lua 1000"... done +running "lua ./spectral-norm.lua 1000"... done +final done diff --git a/lua_benchmark/tests/Lua-Benchmarks/results/speedup_luajit.png b/lua_benchmark/tests/Lua-Benchmarks/results/speedup_luajit.png Binary files differnew file mode 100644 index 0000000..6f012f5 --- /dev/null +++ b/lua_benchmark/tests/Lua-Benchmarks/results/speedup_luajit.png diff --git a/lua_benchmark/tests/Lua-Benchmarks/results/speedup_luajit.txt b/lua_benchmark/tests/Lua-Benchmarks/results/speedup_luajit.txt new file mode 100644 index 0000000..c2931b9 --- /dev/null +++ b/lua_benchmark/tests/Lua-Benchmarks/results/speedup_luajit.txt @@ -0,0 +1,14 @@ +test lua-5.3.4 luajit-2.0.4-interp luajit-2.0.4 +ack 1.0 2.5783972125436 11.472868217054 +fixpoint-fact 1.0 2.5550351288056 2.5580304806565 +heapsort 1.0 2.0149880095923 5.3264659270998 +mandelbrot 1.0 2.0203703703704 18.491525423729 +juliaset 1.0 2.186790505676 3.4851973684211 +queen 1.0 2.1812080536913 11.746987951807 +sieve 1.0 1.7278056951424 10.26368159204 +binary 1.0 3.3795359904819 4.3333333333333 +n-body 1.0 2.279702970297 14.854838709677 +fannkuch 1.0 2.1797410510282 8.9158878504673 +fasta 1.0 1.6870474658085 3.460396039604 +k-nucleotide 1.0 1.7326325411335 3.276577355229 +spectral-norm 1.0 2.2377260981912 17.673469387755 diff --git a/lua_benchmark/tests/Lua-Benchmarks/results/speedup_luajit_log.txt b/lua_benchmark/tests/Lua-Benchmarks/results/speedup_luajit_log.txt new file mode 100644 index 0000000..24fa930 --- /dev/null +++ b/lua_benchmark/tests/Lua-Benchmarks/results/speedup_luajit_log.txt @@ -0,0 +1,43 @@ +Distro: CentOS release 6.8 (Final) +Kernel: 2.6.32-642.13.1.el6.x86_64 +CPU: Intel(R) Core(TM) i7-4770 CPU @ 3.40GHz +running "lua ./ack.lua 3 10"... done +running "luajit -joff ./ack.lua 3 10"... done +running "luajit ./ack.lua 3 10"... done +running "lua ./fixpoint-fact.lua 3000"... done +running "luajit -joff ./fixpoint-fact.lua 3000"... done +running "luajit ./fixpoint-fact.lua 3000"... done +running "lua ./heapsort.lua 10 250000"... done +running "luajit -joff ./heapsort.lua 10 250000"... done +running "luajit ./heapsort.lua 10 250000"... done +running "lua ./mandel.lua"... done +running "luajit -joff ./mandel.lua"... done +running "luajit ./mandel.lua"... done +running "lua ./qt.lua"... done +running "luajit -joff ./qt.lua"... done +running "luajit ./qt.lua"... done +running "lua ./queen.lua 12"... done +running "luajit -joff ./queen.lua 12"... done +running "luajit ./queen.lua 12"... done +running "lua ./sieve.lua 5000"... done +running "luajit -joff ./sieve.lua 5000"... done +running "luajit ./sieve.lua 5000"... done +running "lua ./binary-trees.lua 15"... done +running "luajit -joff ./binary-trees.lua 15"... done +running "luajit ./binary-trees.lua 15"... done +running "lua ./n-body.lua 1000000"... done +running "luajit -joff ./n-body.lua 1000000"... done +running "luajit ./n-body.lua 1000000"... done +running "lua ./fannkuch-redux.lua 10"... done +running "luajit -joff ./fannkuch-redux.lua 10"... done +running "luajit ./fannkuch-redux.lua 10"... done +running "lua ./fasta.lua 2500000"... done +running "luajit -joff ./fasta.lua 2500000"... done +running "luajit ./fasta.lua 2500000"... done +running "lua ./k-nucleotide.lua < fasta1000000.txt"... done +running "luajit -joff ./k-nucleotide.lua < fasta1000000.txt"... done +running "luajit ./k-nucleotide.lua < fasta1000000.txt"... done +running "lua ./spectral-norm.lua 1000"... done +running "luajit -joff ./spectral-norm.lua 1000"... done +running "luajit ./spectral-norm.lua 1000"... done +final done diff --git a/lua_benchmark/tests/Lua-Benchmarks/runbenchmarks.lua b/lua_benchmark/tests/Lua-Benchmarks/runbenchmarks.lua new file mode 100755 index 0000000..dafbb3e --- /dev/null +++ b/lua_benchmark/tests/Lua-Benchmarks/runbenchmarks.lua @@ -0,0 +1,234 @@ +#!/bin/env lua + +-- Configuration --------------------------------------------------------------- + +-- List of binaries that will be tested +local binaries = { + { 'lua-5.3.4', 'lua' }, + { 'luajit-2.0.4-interp', 'luajit -joff' }, + { 'luajit-2.0.4', 'luajit' }, +} + +-- List of tests +local tests_root = './' +local tests = { + { 'ack', 'ack.lua 3 10' }, + { 'fixpoint-fact', 'fixpoint-fact.lua 3000' }, + { 'heapsort', 'heapsort.lua 10 250000' }, + { 'mandelbrot', 'mandel.lua' }, + { 'juliaset', 'qt.lua' }, + { 'queen', 'queen.lua 12' }, + { 'sieve', 'sieve.lua 5000' }, -- Sieve of Eratosthenes + { 'binary', 'binary-trees.lua 15' }, + { 'n-body', 'n-body.lua 1000000' }, + { 'fannkuch', 'fannkuch-redux.lua 10' }, + { 'fasta', 'fasta.lua 2500000' }, + { 'k-nucleotide', 'k-nucleotide.lua < fasta1000000.txt' }, + --{ 'regex-dna', 'regex-dna.lua < fasta1000000.txt' }, + { 'spectral-norm', 'spectral-norm.lua 1000' }, +} + +-- Command line arguments ------------------------------------------------------ + +local nruns = 3 +local supress_errors = true +local basename = 'results' +local normalize = false +local speedup = false +local plot = true + +local usage = [[ +usage: lua ]] .. arg[0] .. [[ [options] +options: + --nruns <n> number of times that each test is executed (default = 3) + --no-supress don't supress error messages from tests + --output <name> name of the benchmark output + --normalize normalize the result based on the first binary + --speedup compute the speedup based on the first binary + --no-plot don't create the plot with gnuplot + --help show this message +]] + +local function parse_args() + local function parse_error(msg) + print('Error: ' .. msg .. '\n' .. usage) + os.exit(1) + end + local function get_next_arg(i) + if i + 1 > #arg then + parse_error(arg[i] .. ' requires a value') + end + local v = arg[i + 1] + arg[i + 1] = nil + return v + end + for i = 1, #arg do + if not arg[i] then goto continue end + if arg[i] == '--nruns' then + nruns = tonumber(get_next_arg(i)) + if not nruns or nruns < 1 then + parse_error('nruns should be a number greater than 1') + end + elseif arg[i] == '--no-supress' then + supress_errors = false + elseif arg[i] == '--output' then + basename = get_next_arg(i) + elseif arg[i] == '--normalize' then + normalize = true + elseif arg[i] == '--speedup' then + speedup = true + elseif arg[i] == '--no-plot' then + plot = false + elseif arg[i] == '--help' then + print(usage) + os.exit() + else + parse_error('invalid argument: ' .. arg[i]) + end + ::continue:: + end +end + +-- Implementation -------------------------------------------------------------- + +-- Run the command a single time and returns the time elapsed +local function measure(cmd) + local time_cmd = '{ TIMEFORMAT=\'%3R\'; time ' .. cmd .. + ' > /dev/null; } 2>&1' + local handle = io.popen(time_cmd) + local result = handle:read("*a") + local time_elapsed = tonumber(result) + handle:close() + if not time_elapsed then + error('Invalid output for "' .. cmd .. '":\n' .. result) + end + return time_elapsed +end + +-- Run the command $nruns and return the fastest time +local function benchmark(cmd) + local min = 999 + io.write('running "' .. cmd .. '"... ') + for _ = 1, nruns do + local time = measure(cmd) + min = math.min(min, time) + end + io.write('done\n') + return min +end + +-- Create a matrix with n rows +local function create_matrix(n) + local m = {} + for i = 1, n do + m[i] = {} + end + return m +end + +-- Measure the time for each binary and test +-- Return a matrix with the result (test x binary) +local function run_all() + local results = create_matrix(#tests) + for i, test in ipairs(tests) do + local test_path = tests_root .. test[2] + for j, binary in ipairs(binaries) do + local cmd = binary[2] .. ' ' .. test_path + local ok, msg = pcall(function() + results[i][j] = benchmark(cmd) + end) + if not ok and not supress_errors then + io.write('error:\n' .. msg .. '\n---\n') + end + end + end + return results +end + +-- Perform an operation for each value in the matrix +local function process_results(results, f) + for _, line in ipairs(results) do + local base = line[1] + for i = 1, #binaries do + line[i] = f(line[i], base) + end + end +end + +-- Print info about the host computer +local function computer_info() + os.execute([[ +echo "Distro: "`cat /etc/*-release | head -1` +echo "Kernel: "`uname -r` +echo "CPU: "`cat /proc/cpuinfo | grep 'model name' | tail -1 | \ + sed 's/model name.*:.//'`]]) +end + +-- Creates and saves the gnuplot data file +local function create_data_file(results) + local data = 'test\t' + for _, binary in ipairs(binaries) do + data = data .. binary[1] .. '\t' + end + data = data .. '\n' + for i, test in ipairs(tests) do + data = data .. test[1] .. '\t' + for j, _ in ipairs(binaries) do + data = data .. results[i][j] .. '\t' + end + data = data .. '\n' + end + io.open(basename .. '.txt', 'w'):write(data):close() +end + +-- Generates the output image with gnuplot +local function generate_image() + local ylabel + if normalize then + ylabel = 'Normalized time' + elseif speedup then + ylabel = 'Speedup' + else + ylabel = 'Elapsed time' + end + os.execute('gnuplot -e "datafile=\'' .. basename .. '.txt\'" ' .. + '-e "outfile=\'' .. basename .. '.png\'" ' .. + '-e "ylabel=\'' .. ylabel .. '\'" ' .. + '-e "nbinaries=' .. #binaries .. '" plot.gpi') +end + +local function setup() + os.execute('luajit ' .. tests_root .. 'fasta.lua 1000000 > fasta1000000.txt') +end + +local function teardown() + os.execute('rm fasta1000000.txt') +end + +local function main() + parse_args() + computer_info() + setup() + local results = run_all() + teardown() + local function f(v, base) + if not v then + return 0 + elseif not base then + return v + elseif speedup then + return base / v + elseif normalize then + return v / base + else + return v + end + end + process_results(results, f) + create_data_file(results) + if plot then generate_image() end + print('final done') +end + +main() + diff --git a/lua_benchmark/tests/Lua-Benchmarks/scimark.lua b/lua_benchmark/tests/Lua-Benchmarks/scimark.lua new file mode 100644 index 0000000..b833b30 --- /dev/null +++ b/lua_benchmark/tests/Lua-Benchmarks/scimark.lua @@ -0,0 +1,433 @@ +------------------------------------------------------------------------------ +-- Lua SciMark (2010-12-20). +-- +-- A literal translation of SciMark 2.0a, written in Java and C. +-- Credits go to the original authors Roldan Pozo and Bruce Miller. +-- See: http://math.nist.gov/scimark2/ +------------------------------------------------------------------------------ +-- Copyright (C) 2006-2010 Mike Pall. All rights reserved. +-- +-- Permission is hereby granted, free of charge, to any person obtaining +-- a copy of this software and associated documentation files (the +-- "Software"), to deal in the Software without restriction, including +-- without limitation the rights to use, copy, modify, merge, publish, +-- distribute, sublicense, and/or sell copies of the Software, and to +-- permit persons to whom the Software is furnished to do so, subject to +-- the following conditions: +-- +-- The above copyright notice and this permission notice shall be +-- included in all copies or substantial portions of the Software. +-- +-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +-- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +-- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +-- IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +-- CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +-- TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +-- SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +-- +-- [ MIT license: http://www.opensource.org/licenses/mit-license.php ] +------------------------------------------------------------------------------ + +------------------------------------------------------------------------------ +-- Modificatin to be compatible with Lua 5.3 +------------------------------------------------------------------------------ + +if table and table.unpack then + unpack = table.unpack +end + +------------------------------------------------------------------------------ + +local SCIMARK_VERSION = "2010-12-10" +local SCIMARK_COPYRIGHT = "Copyright (C) 2006-2010 Mike Pall" + +local MIN_TIME = 2.0 +local RANDOM_SEED = 101009 -- Must be odd. +local SIZE_SELECT = "small" + +local benchmarks = { + "FFT", "SOR", "MC", "SPARSE", "LU", + small = { + FFT = { 1024 }, + SOR = { 100 }, + MC = { }, + SPARSE = { 1000, 5000 }, + LU = { 100 }, + }, + large = { + FFT = { 1048576 }, + SOR = { 1000 }, + MC = { }, + SPARSE = { 100000, 1000000 }, + LU = { 1000 }, + }, +} + +local abs, log, sin, floor = math.abs, math.log, math.sin, math.floor +local pi, clock = math.pi, os.clock +local format = string.format + +------------------------------------------------------------------------------ +-- Select array type: Lua tables or native (FFI) arrays +------------------------------------------------------------------------------ + +local darray, iarray + +local function array_init() + if jit and jit.status and jit.status() then + local ok, ffi = pcall(require, "ffi") + if ok then + darray = ffi.typeof("double[?]") + iarray = ffi.typeof("int[?]") + return + end + end + function darray(n) return {} end + iarray = darray +end + +------------------------------------------------------------------------------ +-- This is a Lagged Fibonacci Pseudo-random Number Generator with +-- j, k, M = 5, 17, 31. Pretty weak, but same as C/Java SciMark. +------------------------------------------------------------------------------ + +local rand, rand_init + +if jit and jit.status and jit.status() then + -- LJ2 has bit operations and zero-based arrays (internally). + local bit = require("bit") + local band, sar = bit.band, bit.arshift + function rand_init(seed) + local Rm, Rj, Ri = iarray(17), 16, 11 + for i=0,16 do Rm[i] = 0 end + for i=16,0,-1 do + seed = band(seed*9069, 0x7fffffff) + Rm[i] = seed + end + function rand() + local i = band(Ri+1, sar(Ri-16, 31)) + local j = band(Rj+1, sar(Rj-16, 31)) + Ri, Rj = i, j + local k = band(Rm[i] - Rm[j], 0x7fffffff) + Rm[j] = k + return k * (1.0/2147483647.0) + end + end +else + -- Better for standard Lua with one-based arrays and without bit operations. + function rand_init(seed) + local Rm, Rj = {}, 1 + for i=1,17 do Rm[i] = 0 end + for i=17,1,-1 do + seed = (seed*9069) % (2^31) + Rm[i] = seed + end + function rand() + local j, m = Rj, Rm + local h = j - 5 + if h < 1 then h = h + 17 end + local k = m[h] - m[j] + if k < 0 then k = k + 2147483647 end + m[j] = k + if j < 17 then Rj = j + 1 else Rj = 1 end + return k * (1.0/2147483647.0) + end + end +end + +local function random_vector(n) + local v = darray(n+1) + for x=1,n do v[x] = rand() end + return v +end + +local function random_matrix(m, n) + local a = {} + for y=1,m do + local v = darray(n+1) + a[y] = v + for x=1,n do v[x] = rand() end + end + return a +end + +------------------------------------------------------------------------------ +-- FFT: Fast Fourier Transform. +------------------------------------------------------------------------------ + +local function fft_bitreverse(v, n) + local j = 0 + for i=0,2*n-4,2 do + if i < j then + v[i+1], v[i+2], v[j+1], v[j+2] = v[j+1], v[j+2], v[i+1], v[i+2] + end + local k = n + while k <= j do j = j - k; k = k / 2 end + j = j + k + end +end + +local function fft_transform(v, n, dir) + if n <= 1 then return end + fft_bitreverse(v, n) + local dual = 1 + repeat + local dual2 = 2*dual + for i=1,2*n-1,2*dual2 do + local j = i+dual2 + local ir, ii = v[i], v[i+1] + local jr, ji = v[j], v[j+1] + v[j], v[j+1] = ir - jr, ii - ji + v[i], v[i+1] = ir + jr, ii + ji + end + local theta = dir * pi / dual + local s, s2 = sin(theta), 2.0 * sin(theta * 0.5)^2 + local wr, wi = 1.0, 0.0 + for a=3,dual2-1,2 do + wr, wi = wr - s*wi - s2*wr, wi + s*wr - s2*wi + for i=a,a+2*(n-dual2),2*dual2 do + local j = i+dual2 + local jr, ji = v[j], v[j+1] + local dr, di = wr*jr - wi*ji, wr*ji + wi*jr + local ir, ii = v[i], v[i+1] + v[j], v[j+1] = ir - dr, ii - di + v[i], v[i+1] = ir + dr, ii + di + end + end + dual = dual2 + until dual >= n +end + +function benchmarks.FFT(n) + local l2n = log(n)/log(2) + if l2n % 1 ~= 0 then + io.stderr:write("Error: FFT data length is not a power of 2\n") + os.exit(1) + end + local v = random_vector(n*2) + return function(cycles) + local norm = 1.0 / n + for p=1,cycles do + fft_transform(v, n, -1) + fft_transform(v, n, 1) + for i=1,n*2 do v[i] = v[i] * norm end + end + return ((5*n-2)*l2n + 2*(n+1)) * cycles + end +end + +------------------------------------------------------------------------------ +-- SOR: Jacobi Successive Over-Relaxation. +------------------------------------------------------------------------------ + +local function sor_run(mat, m, n, cycles, omega) + local om4, om1 = omega*0.25, 1.0-omega + m = m - 1 + n = n - 1 + for i=1,cycles do + for y=2,m do + local v, vp, vn = mat[y], mat[y-1], mat[y+1] + for x=2,n do + v[x] = om4*((vp[x]+vn[x])+(v[x-1]+v[x+1])) + om1*v[x] + end + end + end +end + +function benchmarks.SOR(n) + local mat = random_matrix(n, n) + return function(cycles) + sor_run(mat, n, n, cycles, 1.25) + return (n-1)*(n-1)*cycles*6 + end +end + +------------------------------------------------------------------------------ +-- MC: Monte Carlo Integration. +------------------------------------------------------------------------------ + +local function mc_integrate(cycles) + local under_curve = 0 + local rand = rand + for i=1,cycles do + local x = rand() + local y = rand() + if x*x + y*y <= 1.0 then under_curve = under_curve + 1 end + end + return (under_curve/cycles) * 4 +end + +function benchmarks.MC() + return function(cycles) + local res = mc_integrate(cycles) + assert(math.sqrt(cycles)*math.abs(res-math.pi) < 5.0, "bad MC result") + return cycles * 4 -- Way off, but same as SciMark in C/Java. + end +end + +------------------------------------------------------------------------------ +-- Sparse Matrix Multiplication. +------------------------------------------------------------------------------ + +local function sparse_mult(n, cycles, vy, val, row, col, vx) + for p=1,cycles do + for r=1,n do + local sum = 0 + for i=row[r],row[r+1]-1 do sum = sum + vx[col[i]] * val[i] end + vy[r] = sum + end + end +end + +function benchmarks.SPARSE(n, nz) + local nr = floor(nz/n) + local anz = nr*n + local vx = random_vector(n) + local val = random_vector(anz) + local vy, col, row = darray(n+1), iarray(nz+1), iarray(n+2) + row[1] = 1 + for r=1,n do + local step = floor(r/nr) + if step < 1 then step = 1 end + local rr = row[r] + row[r+1] = rr+nr + for i=0,nr-1 do col[rr+i] = 1+i*step end + end + return function(cycles) + sparse_mult(n, cycles, vy, val, row, col, vx) + return anz*cycles*2 + end +end + +------------------------------------------------------------------------------ +-- LU: Dense Matrix Factorization. +------------------------------------------------------------------------------ + +local function lu_factor(a, pivot, m, n) + local min_m_n = m < n and m or n + for j=1,min_m_n do + local jp, t = j, abs(a[j][j]) + for i=j+1,m do + local ab = abs(a[i][j]) + if ab > t then + jp = i + t = ab + end + end + pivot[j] = jp + if a[jp][j] == 0 then error("zero pivot") end + if jp ~= j then a[j], a[jp] = a[jp], a[j] end + if j < m then + local recp = 1.0 / a[j][j] + for k=j+1,m do + local v = a[k] + v[j] = v[j] * recp + end + end + if j < min_m_n then + for i=j+1,m do + local vi, vj = a[i], a[j] + local eij = vi[j] + for k=j+1,n do vi[k] = vi[k] - eij * vj[k] end + end + end + end +end + +local function matrix_alloc(m, n) + local a = {} + for y=1,m do a[y] = darray(n+1) end + return a +end + +local function matrix_copy(dst, src, m, n) + for y=1,m do + local vd, vs = dst[y], src[y] + for x=1,n do vd[x] = vs[x] end + end +end + +function benchmarks.LU(n) + local mat = random_matrix(n, n) + local tmp = matrix_alloc(n, n) + local pivot = iarray(n+1) + return function(cycles) + for i=1,cycles do + matrix_copy(tmp, mat, n, n) + lu_factor(tmp, pivot, n, n) + end + return 2.0/3.0*n*n*n*cycles + end +end + +------------------------------------------------------------------------------ +-- Main program. +------------------------------------------------------------------------------ + +local function printf(...) + io.write(format(...)) +end + +local function fmtparams(p1, p2) + if p2 then return format("[%d, %d]", p1, p2) + elseif p1 then return format("[%d]", p1) end + return "" +end + +local function measure(min_time, name, ...) + array_init() + rand_init(RANDOM_SEED) + local run = benchmarks[name](...) + local cycles = 1 + repeat + local tm = clock() + local flops = run(cycles, ...) + tm = clock() - tm + if tm >= min_time then + local res = flops / tm * 1.0e-6 + local p1, p2 = ... + printf("%-7s %8.2f %s\n", name, res, fmtparams(...)) + return res + end + cycles = cycles * 2 + until false +end + +printf("Lua SciMark %s based on SciMark 2.0a. %s.\n\n", + SCIMARK_VERSION, SCIMARK_COPYRIGHT) + +while arg and arg[1] do + local a = table.remove(arg, 1) + if a == "-noffi" then + package.preload.ffi = nil + elseif a == "-small" then + SIZE_SELECT = "small" + elseif a == "-large" then + SIZE_SELECT = "large" + elseif benchmarks[a] then + local p = benchmarks[SIZE_SELECT][a] + measure(MIN_TIME, a, tonumber(arg[1]) or p[1], tonumber(arg[2]) or p[2]) + return + else + printf("Usage: scimark [-noffi] [-small|-large] [BENCH params...]\n\n") + printf("BENCH -small -large\n") + printf("---------------------------------------\n") + for _,name in ipairs(benchmarks) do + printf("%-7s %-13s %s\n", name, + fmtparams(unpack(benchmarks.small[name])), + fmtparams(unpack(benchmarks.large[name]))) + end + printf("\n") + os.exit(1) + end +end + +local params = benchmarks[SIZE_SELECT] +local sum = 0 +for _,name in ipairs(benchmarks) do + sum = sum + measure(MIN_TIME, name, unpack(params[name])) +end +printf("\nSciMark %8.2f [%s problem sizes]\n", sum / #benchmarks, SIZE_SELECT) +io.flush() + diff --git a/lua_benchmark/tests/Lua-Benchmarks/sieve.lua b/lua_benchmark/tests/Lua-Benchmarks/sieve.lua new file mode 100644 index 0000000..e2c1487 --- /dev/null +++ b/lua_benchmark/tests/Lua-Benchmarks/sieve.lua @@ -0,0 +1,33 @@ +-- $Id: sieve.lua,v 1.9 2001/05/06 04:37:45 doug Exp $
+-- http://www.bagley.org/~doug/shootout/
+--
+-- Roberto Ierusalimschy pointed out the for loop is much
+-- faster for our purposes here than using a while loop.
+
+local count = 0
+
+function main(num, lim)
+ local flags = {}
+ for num=num,1,-1 do
+ count = 0
+ for i=1,lim do
+ flags[i] = 1
+ end
+ for i=2,lim do
+ if flags[i] == 1 then
+ k = 0
+ for k=i+i, lim, i do
+ flags[k] = 0
+ end
+ count = count + 1
+ end
+ end
+ end
+end
+
+NUM = tonumber((arg and arg[1])) or 100
+lim = (arg and arg[2]) or 8192
+print(NUM,lim)
+count = 0
+main(NUM, lim)
+print("Count: ", count)
diff --git a/lua_benchmark/tests/Lua-Benchmarks/spectral-norm.lua b/lua_benchmark/tests/Lua-Benchmarks/spectral-norm.lua new file mode 100644 index 0000000..5ba652f --- /dev/null +++ b/lua_benchmark/tests/Lua-Benchmarks/spectral-norm.lua @@ -0,0 +1,43 @@ +-- The Computer Language Benchmarks Game +-- http://benchmarksgame.alioth.debian.org/ +-- contributed by Mike Pall + +local function A(i, j) + local ij = i+j-1 + return 1.0 / (ij * (ij-1) * 0.5 + i) +end + +local function Av(x, y, N) + for i=1,N do + local a = 0 + for j=1,N do a = a + x[j] * A(i, j) end + y[i] = a + end +end + +local function Atv(x, y, N) + for i=1,N do + local a = 0 + for j=1,N do a = a + x[j] * A(j, i) end + y[i] = a + end +end + +local function AtAv(x, y, t, N) + Av(x, t, N) + Atv(t, y, N) +end + +local N = tonumber(arg and arg[1]) or 100 +local u, v, t = {}, {}, {} +for i=1,N do u[i] = 1 end + +for i=1,10 do AtAv(u, v, t, N) AtAv(v, u, t, N) end + +local vBv, vv = 0, 0 +for i=1,N do + local ui, vi = u[i], v[i] + vBv = vBv + ui*vi + vv = vv + vi*vi +end +io.write(string.format("%0.9f\n", math.sqrt(vBv / vv))) |