summaryrefslogtreecommitdiff
path: root/tests/src/JIT/Performance/CodeQuality/BenchmarksGame/fasta/fasta-serial.cs
blob: aa5fc0704fdd716b25b77c9f4eca9920586ea24e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

// Adapted from fasta C# .NET Core #2 program
// http://benchmarksgame.alioth.debian.org/u64q/program.php?test=fasta&lang=csharpcore&id=2
// Best-scoring single-threaded C# .NET Core version as of 2017-09-01

/* The Computer Language Benchmarks Game
   http://benchmarksgame.alioth.debian.org/

   contributed by Isaac Gouy
   optimizations by Alp Toker <alp@atoker.com>
*/

using System;
using System.IO;
using System.Text;

namespace BenchmarksGame
{
    class Fasta
    {
        static void Main(string[] args)
        {
            MakeCumulative(HomoSapiens);
            MakeCumulative(IUB);

            int n = args.Length > 0 ? Int32.Parse(args[0]) : 1000;

            using (Stream s = Console.OpenStandardOutput())
            {
                MakeRepeatFasta("ONE", "Homo sapiens alu", Encoding.ASCII.GetBytes(ALU), n * 2, s);
                MakeRandomFasta("TWO", "IUB ambiguity codes", IUB, n * 3, s);
                MakeRandomFasta("THREE", "Homo sapiens frequency", HomoSapiens, n * 5, s);
            }
        }

        // The usual pseudo-random number generator

        const int IM = 139968;
        const int IA = 3877;
        const int IC = 29573;
        static int seed = 42;

        static double random(double max)
        {
            return max * ((seed = (seed * IA + IC) % IM) * (1.0 / IM));
        }

        // Weighted selection from alphabet

        static string ALU =
            "GGCCGGGCGCGGTGGCTCACGCCTGTAATCCCAGCACTTTGG" +
            "GAGGCCGAGGCGGGCGGATCACCTGAGGTCAGGAGTTCGAGA" +
            "CCAGCCTGGCCAACATGGTGAAACCCCGTCTCTACTAAAAAT" +
            "ACAAAAATTAGCCGGGCGTGGTGGCGCGCGCCTGTAATCCCA" +
            "GCTACTCGGGAGGCTGAGGCAGGAGAATCGCTTGAACCCGGG" +
            "AGGCGGAGGTTGCAGTGAGCCGAGATCGCGCCACTGCACTCC" +
            "AGCCTGGGCGACAGAGCGAGACTCCGTCTCAAAAA";

        class Frequency
        {
            public byte c;
            public double p;

            public Frequency(char c, double p)
            {
                this.c = (byte)c;
                this.p = p;
            }
        }

        static Frequency[] IUB = {
        new Frequency ('a', 0.27)
            ,new Frequency ('c', 0.12)
            ,new Frequency ('g', 0.12)
            ,new Frequency ('t', 0.27)

            ,new Frequency ('B', 0.02)
            ,new Frequency ('D', 0.02)
            ,new Frequency ('H', 0.02)
            ,new Frequency ('K', 0.02)
            ,new Frequency ('M', 0.02)
            ,new Frequency ('N', 0.02)
            ,new Frequency ('R', 0.02)
            ,new Frequency ('S', 0.02)
            ,new Frequency ('V', 0.02)
            ,new Frequency ('W', 0.02)
            ,new Frequency ('Y', 0.02)
    };

        static Frequency[] HomoSapiens = {
        new Frequency ('a', 0.3029549426680)
            ,new Frequency ('c', 0.1979883004921)
            ,new Frequency ('g', 0.1975473066391)
            ,new Frequency ('t', 0.3015094502008)
    };

        static void MakeCumulative(Frequency[] a)
        {
            double cp = 0.0;
            for (int i = 0; i < a.Length; i++)
            {
                cp += a[i].p;
                a[i].p = cp;
            }
        }

        // naive
        static byte SelectRandom(Frequency[] a)
        {
            double r = random(1.0);

            for (int i = 0; i < a.Length; i++)
                if (r < a[i].p)
                    return a[i].c;

            return a[a.Length - 1].c;
        }

        const int LineLength = 60;
        static int index = 0;
        static byte[] buf = new byte[1024];

        static void MakeRandomFasta(string id, string desc, Frequency[] a, int n, Stream s)
        {
            index = 0;
            int m = 0;

            byte[] descStr = Encoding.ASCII.GetBytes(">" + id + " " + desc + "\n");
            s.Write(descStr, 0, descStr.Length);

            while (n > 0)
            {
                m = n < LineLength ? n : LineLength;

                if (buf.Length - index < m)
                {
                    s.Write(buf, 0, index);
                    index = 0;
                }

                for (int i = 0; i < m; i++)
                {
                    buf[index++] = SelectRandom(a);
                }

                buf[index++] = (byte)'\n';
                n -= LineLength;
            }

            if (index != 0)
                s.Write(buf, 0, index);
        }

        static void MakeRepeatFasta(string id, string desc, byte[] alu, int n, Stream s)
        {
            index = 0;
            int m = 0;
            int k = 0;
            int kn = alu.Length;

            byte[] descStr = Encoding.ASCII.GetBytes(">" + id + " " + desc + "\n");
            s.Write(descStr, 0, descStr.Length);

            while (n > 0)
            {
                m = n < LineLength ? n : LineLength;

                if (buf.Length - index < m)
                {
                    s.Write(buf, 0, index);
                    index = 0;
                }

                for (int i = 0; i < m; i++)
                {
                    if (k == kn)
                        k = 0;

                    buf[index++] = alu[k];
                    k++;
                }

                buf[index++] = (byte)'\n';
                n -= LineLength;
            }

            if (index != 0)
                s.Write(buf, 0, index);
        }
    }
}