Commit eff714bd

eff714bd1040Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 3 files · +2 added · ~1 modified

Description

feat: add fasta benchmark (6.1x vs Ruby)

Fix float_array literal compilation in compile_array_literal and add fasta DNA sequence generation benchmark. Rewrites global variable usage to pass mutable state array as parameter.

Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]

Details

Commit
eff714bd1040
Parent
15e86b2a6b2b

benchmark/bm_fasta.rbaddedView file

1+# The Computer Language Benchmarks Game - Fasta
2+# Generate random DNA sequences
3+
4+IM = 139968
5+IA = 3877
6+IC = 29573
7+
8+# Mutable state via array cell (no global variables)
9+def gen_random(max, state)
10+ state[0] = (state[0] * IA + IC) % IM
11+ max * state[0] / IM
12+end
13+
14+def make_repeat_fasta(id, desc, src, n)
15+ puts ">" + id + " " + desc
16+ k = 0
17+ i = 0
18+ while i < n
19+ len = 60
20+ if n - i < 60
21+ len = n - i
22+ end
23+ line = ""
24+ j = 0
25+ while j < len
26+ line = line + src[k % src.length]
27+ k = k + 1
28+ j = j + 1
29+ end
30+ puts line
31+ i = i + len
32+ end
33+end
34+
35+def make_random_fasta(id, desc, table_chars, table_probs, n, state)
36+ puts ">" + id + " " + desc
37+ # Build cumulative probabilities
38+ cp = Array.new(table_probs.length, 0.0)
39+ sum = 0.0
40+ k = 0
41+ while k < table_probs.length
42+ sum = sum + table_probs[k]
43+ cp[k] = sum
44+ k = k + 1
45+ end
46+ i = 0
47+ while i < n
48+ len = 60
49+ if n - i < 60
50+ len = n - i
51+ end
52+ line = ""
53+ j = 0
54+ while j < len
55+ r = gen_random(1.0, state)
56+ # Linear search
57+ c = 0
58+ while c < cp.length - 1
59+ if r < cp[c]
60+ break
61+ end
62+ c = c + 1
63+ end
64+ line = line + table_chars[c]
65+ j = j + 1
66+ end
67+ puts line
68+ i = i + len
69+ end
70+end
71+
72+ALU = "GGCCGGGCGCGGTGGCTCACGCCTGTAATCCCAGCACTTTGGGAGGCCGAGGCGGGCGGATCACCTGAGGTCAGGAGTTCGAGACCAGCCTGGCCAACATGGTGAAACCCCGTCTCTACTAAAAATACAAAAATTAGCCGGGCGTGGTGGCGCGCGCCTGTAATCCCAGCTACTCGGGAGGCTGAGGCAGGAGAATCGCTTGAACCCGGGAGGCGGAGGTTGCAGTGAGCCGAGATCGCGCCACTGCACTCCAGCCTGGGCGACAGAGCGAGACTCCGTCTCAAAAAAA"
73+
74+n = Integer(ARGV[0] || 1000)
75+state = [42]
76+
77+make_repeat_fasta("ONE", "Homo sapiens alu", ALU, n * 2)
78+
79+iub_chars = ["a", "c", "g", "t", "B", "D", "H", "K", "M", "N", "R", "S", "V", "W", "Y"]
80+iub_probs = [0.27, 0.12, 0.12, 0.27, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02]
81+
82+make_random_fasta("TWO", "IUB ambiguity codes", iub_chars, iub_probs, n * 3, state)
83+
84+homo_chars = ["a", "c", "g", "t"]
85+homo_probs = [0.3029549426680, 0.1979883004921, 0.1975473066391, 0.3015094502008]
86+
87+make_random_fasta("THREE", "Homo sapiens frequency", homo_chars, homo_probs, n * 5, state)