Skip to content

Commit 77e44c0

Browse files
authored
Merge branch 'main' into main
2 parents b89b014 + 0c70f6f commit 77e44c0

File tree

17 files changed

+648
-60
lines changed

17 files changed

+648
-60
lines changed

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ _The 1st step is to build source code from various of languages_
7272
```bash
7373
cd bench
7474
# To build a subset
75-
dotnet run -p tool -- --task build --langs lisp go --problems nbody helloworld --force-rebuild
75+
dotnet run --project tool -- --task build --langs lisp go --problems nbody helloworld --force-rebuild
7676
# To build all
77-
dotnet run -p tool -- --task build
77+
dotnet run --project tool -- --task build
7878
```
7979

8080
## Test
@@ -84,9 +84,9 @@ _The 2nd step is to test built binaries to ensure the correctness of their imple
8484
```bash
8585
cd bench
8686
# To test a subset
87-
dotnet run -p tool -- --task test --langs lisp go --problems nbody helloworld
87+
dotnet run --project tool -- --task test --langs lisp go --problems nbody helloworld
8888
# To test all
89-
dotnet run -p tool -- --task test
89+
dotnet run --project tool -- --task test
9090
```
9191

9292
## Bench
@@ -96,16 +96,16 @@ _The 3rd step is to generate benchmarks_
9696
```bash
9797
cd bench
9898
# To bench a subset
99-
dotnet run -p tool -- --task bench --langs lisp go --problems nbody helloworld
99+
dotnet run --project tool -- --task bench --langs lisp go --problems nbody helloworld
100100
# To bench all
101-
dotnet run -p tool -- --task bench
101+
dotnet run --project tool -- --task bench
102102
```
103103

104104
_For usage_
105105

106106
```bash
107107
cd bench
108-
dotnet run -p tool -- -h
108+
dotnet run --project tool -- -h
109109

110110
BenchTool
111111
Main function

bench/algorithm/fasta/1.d

Lines changed: 22 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
// translation from zig version. From https://github.com/cyrusmsk/lang_benchmark
2-
31
import std;
4-
import std.outbuffer: OutBuffer;
52

63
immutable maxLine = 60;
74
immutable im = 139_968;
@@ -11,29 +8,28 @@ immutable ic = 29_573;
118
uint seed = 42;
129

1310
static struct AminoAcid {
14-
char l;
11+
ubyte l;
1512
double p;
1613
}
1714

1815
double nextRandom(double max) {
1916
seed = (seed * ia + ic) % im;
20-
return max * seed/im;
17+
return max * seed / im;
2118
}
2219

23-
void repeatAndWrap (OutBuffer b, immutable char[] seq, size_t count) {
20+
void repeatAndWrap (immutable ubyte[] seq, size_t count) {
2421
uint len = cast(uint) seq.length;
25-
char[] paddedSeq = new char[](len + maxLine);
22+
ubyte[] paddedSeq = new ubyte[](len + maxLine);
2623
foreach (i, ref e; paddedSeq)
2724
e = seq[i % len];
2825

2926
size_t off, idx;
27+
size_t rem, lineLength;
3028
while (idx < count) {
31-
immutable rem = count - idx;
32-
immutable size_t lineLength = min(maxLine, rem);
29+
rem = count - idx;
30+
lineLength = min(maxLine, rem);
3331

34-
// speed up the writeln with lockWriter
35-
b.write(paddedSeq[off .. off + lineLength]);
36-
b.write("\n");
32+
writeln(cast(string)paddedSeq[off .. off + lineLength]);
3733

3834
off += lineLength;
3935
if (off > len)
@@ -42,20 +38,20 @@ void repeatAndWrap (OutBuffer b, immutable char[] seq, size_t count) {
4238
}
4339
}
4440

45-
void generateAndWrap (OutBuffer b, immutable AminoAcid[] nucleotides, size_t count) {
41+
void generateAndWrap (immutable AminoAcid[] nucleotides, size_t count) {
4642
double cumProb = 0.0;
4743
double[] cumProbTotal = new double[](nucleotides.length);
4844
foreach(i, e; nucleotides) {
4945
cumProb += e.p;
5046
cumProbTotal[i] = cumProb * im;
5147
}
5248

53-
char[] line = new char[](maxLine+1);
54-
line[maxLine] = '\n';
55-
size_t idx;
49+
ubyte[maxLine+1] line; // was new before
50+
line[maxLine] = cast(ubyte)'\n';
51+
size_t idx, rem, lineLength;
5652
while (idx < count) {
57-
immutable rem = count - idx;
58-
immutable size_t lineLength = min(maxLine, rem);
53+
rem = count - idx;
54+
lineLength = min(maxLine, rem);
5955
foreach (ref col; line[0 .. lineLength]) {
6056
immutable r = nextRandom(im);
6157
size_t c;
@@ -65,21 +61,18 @@ void generateAndWrap (OutBuffer b, immutable AminoAcid[] nucleotides, size_t cou
6561
col = nucleotides[c].l;
6662
}
6763
line[lineLength] = '\n';
68-
b.write(line[0 .. lineLength + 1]);
64+
write(cast(string)line[0 .. lineLength + 1]);
6965

7066
idx += lineLength;
7167
}
7268
}
7369

7470
void main(string[] args) {
7571
immutable uint n = args.length > 1 ? args[1].to!uint : 100;
76-
OutBuffer b = new OutBuffer();
7772

78-
static immutable char[72*3 + 71] homoSapiensAlu = "GGCCGGGCGCGGTGGCTCACGCCTGTAATCCCAGCACTTTGGGAGGCCGAGGCGGGCGGATCACCTGAGGTCAGGAGTTCGAGACCAGCCTGGCCAACATGGTGAAACCCCGTCTCTACTAAAAATACAAAAATTAGCCGGGCGTGGTGGCGCGCGCCTGTAATCCCAGCTACTCGGGAGGCTGAGGCAGGAGAATCGCTTGAACCCGGGAGGCGGAGGTTGCAGTGAGCCGAGATCGCGCCACTGCACTCCAGCCTGGGCGACAGAGCGAGACTCCGTCTCAAAAA";
79-
write(">ONE Homo sapiens alu\n");
80-
repeatAndWrap(b, homoSapiensAlu, 2 * n);
81-
write(b);
82-
b.clear();
73+
static immutable(ubyte[72*3 + 71]) homoSapiensAlu = cast(immutable(ubyte[287]))"GGCCGGGCGCGGTGGCTCACGCCTGTAATCCCAGCACTTTGGGAGGCCGAGGCGGGCGGATCACCTGAGGTCAGGAGTTCGAGACCAGCCTGGCCAACATGGTGAAACCCCGTCTCTACTAAAAATACAAAAATTAGCCGGGCGTGGTGGCGCGCGCCTGTAATCCCAGCTACTCGGGAGGCTGAGGCAGGAGAATCGCTTGAACCCGGGAGGCGGAGGTTGCAGTGAGCCGAGATCGCGCCACTGCACTCCAGCCTGGGCGACAGAGCGAGACTCCGTCTCAAAAA";
74+
writeln(">ONE Homo sapiens alu");
75+
repeatAndWrap(homoSapiensAlu, 2 * n);
8376

8477
static immutable AminoAcid[15] iubNucleotideInfo = [
8578
{ l:'a', p: 0.27 },
@@ -98,18 +91,15 @@ void main(string[] args) {
9891
{ l:'W', p: 0.02 },
9992
{ l:'Y', p: 0.02 },
10093
];
101-
write(">TWO IUB ambiguity codes\n");
102-
generateAndWrap(b, iubNucleotideInfo, 3 * n);
103-
write(b);
104-
b.clear();
94+
writeln(">TWO IUB ambiguity codes");
95+
generateAndWrap(iubNucleotideInfo, 3 * n);
10596

10697
static immutable AminoAcid[4] homoSapienNucleotideInfo = [
10798
{ l:'a', p: 0.3029549426680 },
10899
{ l:'c', p: 0.1979883004921 },
109100
{ l:'g', p: 0.1975473066391 },
110101
{ l:'t', p: 0.3015094502008 },
111102
];
112-
write(">THREE Homo sapiens frequency\n");
113-
generateAndWrap(b, homoSapienNucleotideInfo, 5 * n);
114-
write(b);
103+
writeln(">THREE Homo sapiens frequency");
104+
generateAndWrap(homoSapienNucleotideInfo, 5 * n);
115105
}

bench/algorithm/fasta/6.rs

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
// Ported from 1.zig
2+
3+
use std::io::{self, BufWriter, StdoutLock, Write};
4+
5+
const MAX_LINE_LENGTH: usize = 60;
6+
const IM: u32 = 139968;
7+
const IA: u32 = 3877;
8+
const IC: u32 = 29573;
9+
10+
struct AminoAcid {
11+
l: u8,
12+
p: f64,
13+
}
14+
15+
struct Random {
16+
seed: u32,
17+
}
18+
19+
impl Random {
20+
fn new() -> Self {
21+
Self { seed: 42 }
22+
}
23+
24+
fn next(&mut self) -> f64 {
25+
self.seed = (self.seed * IA + IC) % IM;
26+
(IM as f64 * self.seed as f64) / IM as f64
27+
}
28+
}
29+
30+
impl Iterator for Random {
31+
type Item = f64;
32+
33+
fn next(&mut self) -> Option<Self::Item> {
34+
Some(self.next())
35+
}
36+
}
37+
38+
fn repeat_and_wrap(
39+
out: &mut BufWriter<StdoutLock<'static>>,
40+
sequence: &[u8],
41+
count: usize,
42+
) -> io::Result<()> {
43+
let n = sequence.len();
44+
let padded_sequence = (0..(n + MAX_LINE_LENGTH))
45+
.map(|i| sequence[i % n])
46+
.collect::<Vec<_>>();
47+
48+
let mut off = 0;
49+
let mut idx = 0;
50+
while idx < count {
51+
let rem = count - idx;
52+
let line_length = MAX_LINE_LENGTH.min(rem);
53+
out.write_all(&padded_sequence[off..off + line_length])?;
54+
out.write_all(&[b'\n'])?;
55+
56+
off += line_length;
57+
if off > n {
58+
off -= n;
59+
}
60+
idx += line_length;
61+
}
62+
Ok(())
63+
}
64+
65+
fn generate_and_wrap(
66+
random: &mut Random,
67+
out: &mut BufWriter<StdoutLock<'static>>,
68+
nucleotides: &[AminoAcid],
69+
count: usize,
70+
) -> io::Result<()> {
71+
let mut cum_prob = 0.0;
72+
let cum_prob_total = nucleotides
73+
.iter()
74+
.map(|&AminoAcid { l: _, p }| {
75+
cum_prob += p;
76+
cum_prob * IM as f64
77+
})
78+
.collect::<Vec<_>>();
79+
80+
let mut line = [0u8; MAX_LINE_LENGTH + 1];
81+
line[MAX_LINE_LENGTH] = b'\n';
82+
83+
let mut idx = 0;
84+
while idx < count {
85+
let rem = count - idx;
86+
let line_length = MAX_LINE_LENGTH.min(rem);
87+
88+
line[..line_length]
89+
.iter_mut()
90+
.zip(&mut *random)
91+
.for_each(|(col, r)| {
92+
let c = cum_prob_total
93+
.iter()
94+
.fold(0, |acc, &n| acc + (n <= r) as usize);
95+
96+
*col = nucleotides[c].l;
97+
});
98+
99+
line[line_length] = b'\n';
100+
out.write_all(&line[..line_length + 1])?;
101+
idx += line_length;
102+
}
103+
Ok(())
104+
}
105+
106+
pub fn main() -> io::Result<()> {
107+
let stdout = io::stdout();
108+
let mut stdout = io::BufWriter::new(stdout.lock());
109+
let n = std::env::args_os()
110+
.nth(1)
111+
.and_then(|s| s.as_os_str().to_str().and_then(|s| s.parse().ok()))
112+
.unwrap_or(1000);
113+
114+
let homo_sapiens_alu =
115+
b"GGCCGGGCGCGGTGGCTCACGCCTGTAATCCCAGCACTTTGGGAGGCCGAGGCGGGCGGATCACCTGAGGTC\
116+
AGGAGTTCGAGACCAGCCTGGCCAACATGGTGAAACCCCGTCTCTACTAAAAATACAAAAATTAGCCGGGCG\
117+
TGGTGGCGCGCGCCTGTAATCCCAGCTACTCGGGAGGCTGAGGCAGGAGAATCGCTTGAACCCGGGAGGCGG\
118+
AGGTTGCAGTGAGCCGAGATCGCGCCACTGCACTCCAGCCTGGGCGACAGAGCGAGACTCCGTCTCAAAAA";
119+
120+
stdout.write_all(b">ONE Homo sapiens alu\n".as_slice())?;
121+
repeat_and_wrap(&mut stdout, &homo_sapiens_alu[..], 2 * n)?;
122+
let mut random = Random::new();
123+
124+
let iub_nucleotide_info = &[
125+
AminoAcid { l: b'a', p: 0.27 },
126+
AminoAcid { l: b'c', p: 0.12 },
127+
AminoAcid { l: b'g', p: 0.12 },
128+
AminoAcid { l: b't', p: 0.27 },
129+
AminoAcid { l: b'B', p: 0.02 },
130+
AminoAcid { l: b'D', p: 0.02 },
131+
AminoAcid { l: b'H', p: 0.02 },
132+
AminoAcid { l: b'K', p: 0.02 },
133+
AminoAcid { l: b'M', p: 0.02 },
134+
AminoAcid { l: b'N', p: 0.02 },
135+
AminoAcid { l: b'R', p: 0.02 },
136+
AminoAcid { l: b'S', p: 0.02 },
137+
AminoAcid { l: b'V', p: 0.02 },
138+
AminoAcid { l: b'W', p: 0.02 },
139+
AminoAcid { l: b'Y', p: 0.02 },
140+
];
141+
142+
stdout.write_all(b">TWO IUB ambiguity codes\n".as_slice())?;
143+
generate_and_wrap(&mut random, &mut stdout, iub_nucleotide_info, 3 * n)?;
144+
145+
let homo_sapien_nucleotide_info = &[
146+
AminoAcid {
147+
l: b'a',
148+
p: 0.3029549426680,
149+
},
150+
AminoAcid {
151+
l: b'c',
152+
p: 0.1979883004921,
153+
},
154+
AminoAcid {
155+
l: b'g',
156+
p: 0.1975473066391,
157+
},
158+
AminoAcid {
159+
l: b't',
160+
p: 0.3015094502008,
161+
},
162+
];
163+
164+
stdout.write_all(b">THREE Homo sapiens frequency\n".as_slice())?;
165+
generate_and_wrap(&mut random, &mut stdout, homo_sapien_nucleotide_info, 5 * n)?;
166+
Ok(())
167+
}

0 commit comments

Comments
 (0)