1- // translation from zig version. From https://github.com/cyrusmsk/lang_benchmark
2-
31import std;
4- import std.outbuffer : OutBuffer ;
52
63immutable maxLine = 60 ;
74immutable im = 139_968;
@@ -11,29 +8,28 @@ immutable ic = 29_573;
118uint seed = 42 ;
129
1310static struct AminoAcid {
14- char l;
11+ ubyte l;
1512 double p;
1613}
1714
1815double 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
7470void 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}
0 commit comments