summaryrefslogtreecommitdiff
path: root/tests/src/JIT/Performance/CodeQuality/BenchmarksGame/fasta/fasta.csharp-2.cs
blob: 3e97d6442915edc7df27175ecfe89ab0d306d856 (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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// 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.
/* The Computer Language Benchmarks Game
   http://benchmarksgame.alioth.debian.org/

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

   modified for use with xunit-performance
*/

using Microsoft.Xunit.Performance;
using System;
using System.IO;
using System.Text;

[assembly: OptimizeForBenchmarks]

namespace BenchmarksGame
{
public class Fasta
{
#if DEBUG
    private const int Iterations = 1;
#else
    const int Iterations = 800;
#endif

    public static int Main(string[] args)
    {
        MakeCumulative(s_homoSapiens);
        MakeCumulative(s_IUB);

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

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

    [Benchmark]
    public static void Bench()
    {
        int n = 5000;
        foreach (var iteration in Benchmark.Iterations)
        {
            using (iteration.StartMeasurement())
            {
                for (int i = 0; i < Iterations; i++)
                {
                    using (Stream s = Stream.Null)
                    {
                        MakeRepeatFasta("ONE", "Homo sapiens alu", Encoding.ASCII.GetBytes(s_ALU), n * 2, s);
                        MakeRandomFasta("TWO", "IUB ambiguity codes", s_IUB, n * 3, s);
                        MakeRandomFasta("THREE", "Homo sapiens frequency", s_homoSapiens, n * 5, s);
                    }
                }
            }
        }
    }

    // The usual pseudo-random number generator

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

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

    // Weighted selection from alphabet

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

    private class Frequency
    {
        public byte c;
        public double p;

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

    private static Frequency[] s_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)
    };

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

    private 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
    private 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;
    }

    private const int LineLength = 60;
    private static int s_index = 0;
    private static byte[] s_buf = new byte[1024];

    private static void MakeRandomFasta(string id, string desc, Frequency[] a, int n, Stream s)
    {
        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 (s_buf.Length - s_index < m)
            {
                s.Write(s_buf, 0, s_index);
                s_index = 0;
            }

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

            s_buf[s_index++] = (byte)'\n';
            n -= LineLength;
        }

        if (s_index != 0)
            s.Write(s_buf, 0, s_index);
    }

    private static void MakeRepeatFasta(string id, string desc, byte[] alu, int n, Stream s)
    {
        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 (s_buf.Length - s_index < m)
            {
                s.Write(s_buf, 0, s_index);
                s_index = 0;
            }

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

                s_buf[s_index++] = alu[k];
                k++;
            }

            s_buf[s_index++] = (byte)'\n';
            n -= LineLength;
        }

        if (s_index != 0)
            s.Write(s_buf, 0, s_index);
    }
}
}