Skip to content

Commit e87f024

Browse files
committed
Add the benchmark suite
1 parent 843fb7b commit e87f024

19 files changed

+1711
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
results.*

ack.lua

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
-- $Id: ackermann.lua,v 1.5 2000/12/09 20:07:43 doug Exp $
2+
-- http://www.bagley.org/~doug/shootout/
3+
4+
local function Ack(M, N)
5+
if (M == 0) then
6+
return N + 1
7+
end
8+
if (N == 0) then
9+
return Ack(M - 1, 1)
10+
end
11+
return Ack(M - 1, Ack(M, (N - 1)))
12+
end
13+
14+
N = tonumber((arg and arg[1])) or 3
15+
M = tonumber((arg and arg[2])) or 8
16+
print(string.format("Ack(%d, %d) = %d\n", N, M, Ack(N,M)))

binary-trees.lua

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
-- The Computer Language Benchmarks Game
2+
-- http://benchmarksgame.alioth.debian.org/
3+
-- contributed by Mike Pall
4+
5+
local function BottomUpTree(item, depth)
6+
if depth > 0 then
7+
local i = item + item
8+
depth = depth - 1
9+
local left, right = BottomUpTree(i-1, depth), BottomUpTree(i, depth)
10+
return { item, left, right }
11+
else
12+
return { item }
13+
end
14+
end
15+
16+
local function ItemCheck(tree)
17+
if tree[2] then
18+
return tree[1] + ItemCheck(tree[2]) - ItemCheck(tree[3])
19+
else
20+
return tree[1]
21+
end
22+
end
23+
24+
local N = tonumber(arg and arg[1]) or 0
25+
local mindepth = 4
26+
local maxdepth = mindepth + 2
27+
if maxdepth < N then maxdepth = N end
28+
29+
do
30+
local stretchdepth = maxdepth + 1
31+
local stretchtree = BottomUpTree(0, stretchdepth)
32+
io.write(string.format("stretch tree of depth %d\t check: %d\n",
33+
stretchdepth, ItemCheck(stretchtree)))
34+
end
35+
36+
local longlivedtree = BottomUpTree(0, maxdepth)
37+
38+
for depth=mindepth,maxdepth,2 do
39+
local iterations = 2 ^ (maxdepth - depth + mindepth)
40+
local check = 0
41+
for i=1,iterations do
42+
check = check + ItemCheck(BottomUpTree(1, depth)) +
43+
ItemCheck(BottomUpTree(-1, depth))
44+
end
45+
io.write(string.format("%d\t trees of depth %d\t check: %d\n",
46+
iterations*2, depth, check))
47+
end
48+
49+
io.write(string.format("long lived tree of depth %d\t check: %d\n",
50+
maxdepth, ItemCheck(longlivedtree)))

fannkuch-redux.lua

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
-- The Computer Language Benchmarks Game
2+
-- http://benchmarksgame.alioth.debian.org/
3+
-- contributed by Mike Pall
4+
5+
local function fannkuch(n)
6+
local p, q, s, sign, maxflips, sum = {}, {}, {}, 1, 0, 0
7+
for i=1,n do p[i] = i; q[i] = i; s[i] = i end
8+
repeat
9+
-- Copy and flip.
10+
local q1 = p[1] -- Cache 1st element.
11+
if q1 ~= 1 then
12+
for i=2,n do q[i] = p[i] end -- Work on a copy.
13+
local flips = 1
14+
repeat
15+
local qq = q[q1]
16+
if qq == 1 then -- ... until 1st element is 1.
17+
sum = sum + sign*flips
18+
if flips > maxflips then maxflips = flips end -- New maximum?
19+
break
20+
end
21+
q[q1] = q1
22+
if q1 >= 4 then
23+
local i, j = 2, q1 - 1
24+
repeat q[i], q[j] = q[j], q[i]; i = i + 1; j = j - 1; until i >= j
25+
end
26+
q1 = qq; flips = flips + 1
27+
until false
28+
end
29+
-- Permute.
30+
if sign == 1 then
31+
p[2], p[1] = p[1], p[2]; sign = -1 -- Rotate 1<-2.
32+
else
33+
p[2], p[3] = p[3], p[2]; sign = 1 -- Rotate 1<-2 and 1<-2<-3.
34+
for i=3,n do
35+
local sx = s[i]
36+
if sx ~= 1 then s[i] = sx-1; break end
37+
if i == n then return sum, maxflips end -- Out of permutations.
38+
s[i] = i
39+
-- Rotate 1<-...<-i+1.
40+
local t = p[1]; for j=1,i do p[j] = p[j+1] end; p[i+1] = t
41+
end
42+
end
43+
until false
44+
end
45+
46+
local n = tonumber(arg and arg[1]) or 7
47+
local sum, flips = fannkuch(n)
48+
io.write(sum, "\nPfannkuchen(", n, ") = ", flips, "\n")

fasta.lua

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
-- The Computer Language Benchmarks Game
2+
-- http://benchmarksgame.alioth.debian.org/
3+
-- contributed by Mike Pall
4+
5+
-- Compability with Lua 5.3
6+
if not loadstring then
7+
loadstring = load
8+
end
9+
if not unpack then
10+
unpack = table.unpack
11+
end
12+
13+
local Last = 42
14+
local function random(max)
15+
local y = (Last * 3877 + 29573) % 139968
16+
Last = y
17+
return (max * y) / 139968
18+
end
19+
20+
local function make_repeat_fasta(id, desc, s, n)
21+
local write, sub = io.write, string.sub
22+
write(">", id, " ", desc, "\n")
23+
local p, sn, s2 = 1, #s, s..s
24+
for i=60,n,60 do
25+
write(sub(s2, p, p + 59), "\n")
26+
p = p + 60; if p > sn then p = p - sn end
27+
end
28+
local tail = n % 60
29+
if tail > 0 then write(sub(s2, p, p + tail-1), "\n") end
30+
end
31+
32+
local function make_random_fasta(id, desc, bs, n)
33+
io.write(">", id, " ", desc, "\n")
34+
loadstring([=[
35+
local write, char, unpack, n, random = io.write, string.char, unpack, ...
36+
local buf, p = {}, 1
37+
for i=60,n,60 do
38+
for j=p,p+59 do ]=]..bs..[=[ end
39+
buf[p+60] = 10; p = p + 61
40+
if p >= 2048 then write(char(unpack(buf, 1, p-1))); p = 1 end
41+
end
42+
local tail = n % 60
43+
if tail > 0 then
44+
for j=p,p+tail-1 do ]=]..bs..[=[ end
45+
p = p + tail; buf[p] = 10; p = p + 1
46+
end
47+
write(char(unpack(buf, 1, p-1)))
48+
]=], desc)(n, random)
49+
end
50+
51+
local function bisect(c, p, lo, hi)
52+
local n = hi - lo
53+
if n == 0 then return "buf[j] = "..c[hi].."\n" end
54+
local mid = math.floor(n / 2)
55+
return "if r < "..p[lo+mid].." then\n"..bisect(c, p, lo, lo+mid)..
56+
"else\n"..bisect(c, p, lo+mid+1, hi).."end\n"
57+
end
58+
59+
local function make_bisect(tab)
60+
local c, p, sum = {}, {}, 0
61+
for i,row in ipairs(tab) do
62+
c[i] = string.byte(row[1])
63+
sum = sum + row[2]
64+
p[i] = sum
65+
end
66+
return "local r = random(1)\n"..bisect(c, p, 1, #tab)
67+
end
68+
69+
local alu =
70+
"GGCCGGGCGCGGTGGCTCACGCCTGTAATCCCAGCACTTTGG"..
71+
"GAGGCCGAGGCGGGCGGATCACCTGAGGTCAGGAGTTCGAGA"..
72+
"CCAGCCTGGCCAACATGGTGAAACCCCGTCTCTACTAAAAAT"..
73+
"ACAAAAATTAGCCGGGCGTGGTGGCGCGCGCCTGTAATCCCA"..
74+
"GCTACTCGGGAGGCTGAGGCAGGAGAATCGCTTGAACCCGGG"..
75+
"AGGCGGAGGTTGCAGTGAGCCGAGATCGCGCCACTGCACTCC"..
76+
"AGCCTGGGCGACAGAGCGAGACTCCGTCTCAAAAA"
77+
78+
local iub = make_bisect{
79+
{ "a", 0.27 },
80+
{ "c", 0.12 },
81+
{ "g", 0.12 },
82+
{ "t", 0.27 },
83+
{ "B", 0.02 },
84+
{ "D", 0.02 },
85+
{ "H", 0.02 },
86+
{ "K", 0.02 },
87+
{ "M", 0.02 },
88+
{ "N", 0.02 },
89+
{ "R", 0.02 },
90+
{ "S", 0.02 },
91+
{ "V", 0.02 },
92+
{ "W", 0.02 },
93+
{ "Y", 0.02 },
94+
}
95+
96+
local homosapiens = make_bisect{
97+
{ "a", 0.3029549426680 },
98+
{ "c", 0.1979883004921 },
99+
{ "g", 0.1975473066391 },
100+
{ "t", 0.3015094502008 },
101+
}
102+
103+
local N = tonumber(arg and arg[1]) or 1000
104+
make_repeat_fasta('ONE', 'Homo sapiens alu', alu, N*2)
105+
make_random_fasta('TWO', 'IUB ambiguity codes', iub, N*3)
106+
make_random_fasta('THREE', 'Homo sapiens frequency', homosapiens, N*5)

fixpoint-fact.lua

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
-- fixed-point operator
2+
local Z = function (le)
3+
local a = function (f)
4+
return le(function (x) return f(f)(x) end)
5+
end
6+
return a(a)
7+
end
8+
9+
10+
-- non-recursive factorial
11+
12+
local F = function (f)
13+
return function (n)
14+
if n == 0 then return 1
15+
else return n*f(n-1) end
16+
end
17+
end
18+
19+
local fat = Z(F)
20+
21+
local s = 0
22+
for i = 1, arg[1] or 100 do s = s + fat(i) end
23+
print(s)
24+

heapsort.lua

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
local random, floor = math.random, math.floor
2+
floor = math.ifloor or floor
3+
4+
function heapsort(n, ra)
5+
local j, i, rra
6+
local l = floor(n/2) + 1
7+
-- local l = (n//2) + 1
8+
local ir = n;
9+
while 1 do
10+
if l > 1 then
11+
l = l - 1
12+
rra = ra[l]
13+
else
14+
rra = ra[ir]
15+
ra[ir] = ra[1]
16+
ir = ir - 1
17+
if (ir == 1) then
18+
ra[1] = rra
19+
return
20+
end
21+
end
22+
i = l
23+
j = l * 2
24+
while j <= ir do
25+
if (j < ir) and (ra[j] < ra[j+1]) then
26+
j = j + 1
27+
end
28+
if rra < ra[j] then
29+
ra[i] = ra[j]
30+
i = j
31+
j = j + i
32+
else
33+
j = ir + 1
34+
end
35+
end
36+
ra[i] = rra
37+
end
38+
end
39+
40+
local Num = tonumber((arg and arg[1])) or 4
41+
for i=1,Num do
42+
local N = tonumber((arg and arg[2])) or 10000
43+
local a = {}
44+
for i=1,N do a[i] = random() end
45+
heapsort(N, a)
46+
for i=1,N-1 do assert(a[i] <= a[i+1]) end
47+
end
48+

k-nucleotide.lua

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
-- The Computer Language Benchmarks Game
2+
-- http://benchmarksgame.alioth.debian.org/
3+
-- contributed by Mike Pall
4+
5+
local function kfrequency(seq, freq, k, frame)
6+
local sub = string.sub
7+
local k1 = k - 1
8+
for i=frame,string.len(seq)-k1,k do
9+
local c = sub(seq, i, i+k1)
10+
freq[c] = freq[c] + 1
11+
end
12+
end
13+
14+
local function freqdefault()
15+
return 0
16+
end
17+
18+
local function count(seq, frag)
19+
local k = string.len(frag)
20+
local freq = setmetatable({}, { __index = freqdefault })
21+
for frame=1,k do kfrequency(seq, freq, k, frame) end
22+
io.write(freq[frag] or 0, "\t", frag, "\n")
23+
end
24+
25+
local function frequency(seq, k)
26+
local freq = setmetatable({}, { __index = freqdefault })
27+
for frame=1,k do kfrequency(seq, freq, k, frame) end
28+
local sfreq, sn = {}, 1
29+
for c,v in pairs(freq) do sfreq[sn] = c; sn = sn + 1 end
30+
table.sort(sfreq, function(a, b)
31+
local fa, fb = freq[a], freq[b]
32+
return fa == fb and a > b or fa > fb
33+
end)
34+
sum = string.len(seq)-k+1
35+
for _,c in ipairs(sfreq) do
36+
io.write(string.format("%s %0.3f\n", c, (freq[c]*100)/sum))
37+
end
38+
io.write("\n")
39+
end
40+
41+
local function readseq()
42+
local sub = string.sub
43+
for line in io.lines() do
44+
if sub(line, 1, 1) == ">" and sub(line, 2, 6) == "THREE" then break end
45+
end
46+
local lines, ln = {}, 0
47+
for line in io.lines() do
48+
local c = sub(line, 1, 1)
49+
if c == ">" then
50+
break
51+
elseif c ~= ";" then
52+
ln = ln + 1
53+
lines[ln] = line
54+
end
55+
end
56+
return string.upper(table.concat(lines, "", 1, ln))
57+
end
58+
59+
local seq = readseq()
60+
frequency(seq, 1)
61+
frequency(seq, 2)
62+
count(seq, "GGT")
63+
count(seq, "GGTA")
64+
count(seq, "GGTATT")
65+
count(seq, "GGTATTTTAATT")
66+
count(seq, "GGTATTTTAATTTATAGT")

0 commit comments

Comments
 (0)