summaryrefslogtreecommitdiff
path: root/tests/src/JIT/Performance/CodeQuality/BenchmarksGame/fastaredux/fastaredux.csharp.cs
blob: df8999a2deaab2255fd4cfd871a7cba8e4d5092c (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
// 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 Robert F. Tobler
   optimized based on java & C# by Enotus, Isaac Gouy, and Alp Toker

   modified for use with xunit-performance
*/

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

[assembly: OptimizeForBenchmarks]
[assembly: MeasureInstructionsRetired]

namespace BenchmarksGame
{
public static class FastaRedux
{
#if DEBUG
    private const int Iterations = 1;
#else
    const int Iterations = 5;
#endif

    public static int Main(string[] args)
    {
        AccumulateAndScale(s_homoSapiens);
        AccumulateAndScale(s_IUB);
        int n = args.Length > 0 ? Int32.Parse(args[0]) : 2500;
        using (Stream s = Console.OpenStandardOutput())
        {
            s.WriteRepeatFasta("ONE", "Homo sapiens alu", Encoding.ASCII.GetBytes(s_ALU), n * 2);
            s.WriteRandomFasta("TWO", "IUB ambiguity codes", s_IUB, n * 3);
            s.WriteRandomFasta("THREE", "Homo sapiens frequency", s_homoSapiens, n * 5);
        }
        return 100;
    }

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

    private const int LINE_LEN = 60;
    private const int BUF_LEN = 64 * 1024;
    private const byte LF = (byte)'\n';

    private const int LOOKUP_LEN = 1024;
    private const double LOOKUP_SCALE = LOOKUP_LEN - 1;

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

    private struct Freq
    {
        public double P;
        public byte C;

        public Freq(char c, double p) { C = (byte)c; P = p; }
    }

    private static Freq[] s_IUB = {
      new Freq('a', 0.27), new Freq('c', 0.12), new Freq('g', 0.12),
      new Freq('t', 0.27), new Freq('B', 0.02), new Freq('D', 0.02),
      new Freq('H', 0.02), new Freq('K', 0.02), new Freq('M', 0.02),
      new Freq('N', 0.02), new Freq('R', 0.02), new Freq('S', 0.02),
      new Freq('V', 0.02), new Freq('W', 0.02), new Freq('Y', 0.02),
    };

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

    private static void AccumulateAndScale(Freq[] a)
    {
        double cp = 0.0;
        for (int i = 0; i < a.Length; i++)
            a[i].P = (cp += a[i].P) * LOOKUP_SCALE;
        a[a.Length - 1].P = LOOKUP_SCALE;
    }

    private static byte[] s_buf = new byte[BUF_LEN];

    private static int WriteDesc(this byte[] buf, string id, string desc)
    {
        var ds = Encoding.ASCII.GetBytes(">" + id + " " + desc + "\n");
        for (int i = 0; i < ds.Length; i++) buf[i] = ds[i];
        return BUF_LEN - ds.Length;
    }

    private static int Min(int a, int b) { return a < b ? a : b; }

    private static void WriteRepeatFasta(
          this Stream s, string id, string desc, byte[] alu, int nr)
    {
        int alen = alu.Length;
        int ar = alen, br = s_buf.WriteDesc(id, desc), lr = LINE_LEN;
        while (nr > 0)
        {
            int r = Min(Min(nr, lr), Min(ar, br));
            for (int ai = alen - ar, bi = BUF_LEN - br, be = bi + r;
                bi < be; bi++, ai++)
                s_buf[bi] = alu[ai];
            nr -= r; lr -= r; br -= r; ar -= r;
            if (ar == 0) ar = alen;
            if (br == 0) { s.Write(s_buf, 0, BUF_LEN); br = BUF_LEN; }
            if (lr == 0) { s_buf[BUF_LEN - (br--)] = LF; lr = LINE_LEN; }
            if (br == 0) { s.Write(s_buf, 0, BUF_LEN); br = BUF_LEN; }
        }
        if (lr < LINE_LEN) s_buf[BUF_LEN - (br--)] = LF;
        if (br < BUF_LEN) s.Write(s_buf, 0, BUF_LEN - br);
    }

    private static Freq[] s_lookup = new Freq[LOOKUP_LEN];

    private static void CreateLookup(Freq[] fr)
    {
        for (int i = 0, j = 0; i < LOOKUP_LEN; i++)
        {
            while (fr[j].P < i) j++;
            s_lookup[i] = fr[j];
        }
    }

    private const int IM = 139968;
    private const int IA = 3877;
    private const int IC = 29573;
    private const double SCALE = LOOKUP_SCALE / IM;

    private static int s_last = 42;

    private static void WriteRandomFasta(
          this Stream s, string id, string desc, Freq[] fr, int nr)
    {
        CreateLookup(fr);
        int br = s_buf.WriteDesc(id, desc), lr = LINE_LEN;
        while (nr > 0)
        {
            int r = Min(Min(nr, lr), br);
            for (int bi = BUF_LEN - br, be = bi + r; bi < be; bi++)
            {
                double p = SCALE * (s_last = (s_last * IA + IC) % IM);
                int ai = (int)p; if (s_lookup[ai].P < p) ai++;
                s_buf[bi] = s_lookup[ai].C;
            }
            nr -= r; lr -= r; br -= r;
            if (br == 0) { s.Write(s_buf, 0, BUF_LEN); br = BUF_LEN; }
            if (lr == 0) { s_buf[BUF_LEN - (br--)] = LF; lr = LINE_LEN; }
            if (br == 0) { s.Write(s_buf, 0, BUF_LEN); br = BUF_LEN; }
        }
        if (lr < LINE_LEN) s_buf[BUF_LEN - (br--)] = LF;
        if (br < BUF_LEN) s.Write(s_buf, 0, BUF_LEN - br);
    }
}
}