The Computer Language
Benchmarks Game

regex-dna C# Mono LLVM #7 program

source code

/* The Computer Language Benchmarks Game
   http://benchmarksgame.alioth.debian.org/
 *
 * contributed by Jimmy Tang
 * modified by Josh Goldfoot
 */

using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Collections.Generic;
using System.Text.RegularExpressions;

class regexdna
{
    static string readStdIn(out int seqLength, out int inputLength)
    {
        StringBuilder sb = new StringBuilder(10000000);
        int commentLength = 0;
        String line;
        using (var r = new StreamReader(Console.OpenStandardInput()))
        {
            while ((line = r.ReadLine()) != null)
            {
                if (line[0] == '>')
                    commentLength += line.Length + 1;
                else
                {
                    sb.Append(line);
                    commentLength += 1;
                }
            }
        }
        seqLength = sb.Length;
        inputLength = seqLength + commentLength;
        return sb.ToString();
    }

    static void Main()
    {
        int seqLength, initialLength;
        var sequence = readStdIn(out seqLength, out initialLength);

        string[] variants = {
           "agggtaaa|tttaccct"
          ,"[cgt]gggtaaa|tttaccc[acg]"
          ,"a[act]ggtaaa|tttacc[agt]t"
          ,"ag[act]gtaaa|tttac[agt]ct"
          ,"agg[act]taaa|ttta[agt]cct"
          ,"aggg[acg]aaa|ttt[cgt]ccct"
          ,"agggt[cgt]aa|tt[acg]accct"
          ,"agggta[cgt]a|t[acg]taccct"
          ,"agggtaa[cgt]|[acg]ttaccct"
        };

        var flags = variants.Select((v, i) =>
        {
            var flag = new ManualResetEvent(false);
            ThreadPool.QueueUserWorkItem(x =>
            {
                Regex r = new Regex(v);
                int count = 0;
                for (Match m = r.Match(sequence); m.Success; m = m.NextMatch()) count++;
                variants[i] += " " + count;
                flag.Set();
            });
            return flag;
        });
        var newSequenceFlag = new ManualResetEvent(false);
        string newSequence = "";
        ThreadPool.QueueUserWorkItem(x =>
        {
            var dict = new Dictionary<string, string> {
                {"B", "(c|g|t)"}, {"D", "(a|g|t)"},   {"H", "(a|c|t)"}, {"K", "(g|t)"},
                {"M", "(a|c)"},   {"N", "(a|c|g|t)"}, {"R", "(a|g)"},   {"S", "(c|g)"},
                {"V", "(a|c|g)"}, {"W", "(a|t)"},     {"Y", "(c|t)"} 
            };
            newSequence = new Regex("[WYKMSRBDVHN]").Replace(sequence, m => dict[m.Value]);
            newSequenceFlag.Set();
        });
        WaitHandle.WaitAll(flags.ToArray());
        newSequenceFlag.WaitOne();

        Console.WriteLine(string.Join("\n", variants));
        Console.WriteLine("\n{0}\n{1}\n{2}", initialLength, seqLength, newSequence.Length);

    }


}
    

notes, command-line, and program output

NOTES:
32-bit Ubuntu one core
Mono JIT compiler version 4.5.1 (master/3e844dd Fri May  6 19:24:07 PDT 2016)
	LLVM:          yes(3.6.0svn-mono-master/9f79399)
	GC:            sgen



Tue, 28 Jun 2016 20:28:31 GMT

MAKE:
mv regexdna.csharpllvm-7.csharpllvm regexdna.csharpllvm-7.cs
/usr/local/bin/mcs  -optimize+ -platform:x86 -out:regexdna.csharpllvm-7.csharpllvm_run regexdna.csharpllvm-7.cs
rm regexdna.csharpllvm-7.cs
0.31s to complete and log all make actions

COMMAND LINE:
/usr/local/bin/mono --llvm --gc=sgen regexdna.csharpllvm-7.csharpllvm_run 0 < regexdna-input5000000.txt

PROGRAM OUTPUT:
agggtaaa|tttaccct 356
[cgt]gggtaaa|tttaccc[acg] 1250
a[act]ggtaaa|tttacc[agt]t 4252
ag[act]gtaaa|tttac[agt]ct 2894
agg[act]taaa|ttta[agt]cct 5435
aggg[acg]aaa|ttt[cgt]ccct 1537
agggt[cgt]aa|tt[acg]accct 1431
agggta[cgt]a|t[acg]taccct 1608
agggtaa[cgt]|[acg]ttaccct 2178

50833411
50000000
66800214