-
-
Notifications
You must be signed in to change notification settings - Fork 346
/
Program.cs
155 lines (140 loc) · 4.57 KB
/
Program.cs
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
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using System.Collections.Generic;
using System.Data.SqlTypes;
using System.Globalization;
using System.Linq;
[MemoryDiagnoser]
public class BenchMe
{
private readonly string strand = "UGGUGUUAUUAAUGGUUU";
private static readonly Dictionary<string, string> lookup = new();
private static void RoboLoad(string protein, params string[] codons)
{
foreach (string codon in codons)
lookup.Add(codon, protein);
}
[Benchmark]
public string[] ProteinsSubstringDict()
{
var length = strand.Length;
List<String> proteins = new();
var endIndex = 3;
while (endIndex <= length)
{
var codon = strand.Substring(endIndex - 3, 3);
var protein = lookup[codon];
switch (protein)
{
case "STOP":
return proteins.ToArray();
default:
proteins.Add(protein);
break;
}
endIndex += 3;
}
return proteins.ToArray();
}
[Benchmark]
public string[] ProteinsSubstringSwitch()
{
var length = strand.Length;
List<String> proteins = new();
var endIndex = 3;
while (endIndex <= length)
{
var codon = strand.Substring(endIndex - 3, 3);
var protein = ToProtein(codon);
switch (protein)
{
case "STOP":
return proteins.ToArray();
default:
proteins.Add(protein);
break;
}
endIndex += 3;
}
return proteins.ToArray();
}
private static readonly IDictionary<string, string> proteins = new Dictionary<string, string>();
static BenchMe ()
{
RoboLoad("Methionine", "AUG");
RoboLoad("Phenylalanine", "UUU", "UUC");
RoboLoad("Leucine", "UUA", "UUG");
RoboLoad("Serine", "UCU", "UCC", "UCA", "UCG");
RoboLoad("Tyrosine", "UAU", "UAC");
RoboLoad("Cysteine", "UGU", "UGC");
RoboLoad("Tryptophan", "UGG");
RoboLoad("STOP", "UAA", "UAG", "UGA");
proteins.Add("AUG", "Methionine");
proteins.Add("UUU", "Phenylalanine");
proteins.Add("UUC", "Phenylalanine");
proteins.Add("UUA", "Leucine");
proteins.Add("UUG", "Leucine");
proteins.Add("UCU", "Serine");
proteins.Add("UCC", "Serine");
proteins.Add("UCA", "Serine");
proteins.Add("UCG", "Serine");
proteins.Add("UAU", "Tyrosine");
proteins.Add("UAC", "Tyrosine");
proteins.Add("UGU", "Cysteine");
proteins.Add("UGC", "Cysteine");
proteins.Add("UGG", "Tryptophan");
proteins.Add("UAA", "STOP");
proteins.Add("UAG", "STOP");
proteins.Add("UGA", "STOP");
}
[Benchmark]
public string[] ProteinsLinqDict()
{
return strand
.Select((_, i) => i)
.Where(i => i % 3 == 0)
.Select(i => proteins[strand.Substring(i, 3)])
.TakeWhile(protein => protein != "STOP")
.ToArray();
}
[Benchmark]
public string[] ProteinsYieldDict() =>
strand.Chunked(3).Select(codon => proteins[codon]).TakeWhile(protein => protein != "STOP").ToArray();
private static string ToProtein(string input) =>
input switch
{
"AUG" => "Methionine",
"UUU" => "Phenylalanine",
"UUC" => "Phenylalanine",
"UUA" => "Leucine",
"UUG" => "Leucine",
"UCU" => "Serine",
"UCC" => "Serine",
"UCA" => "Serine",
"UCG" => "Serine",
"UAU" => "Tyrosine",
"UAC" => "Tyrosine",
"UGU" => "Cysteine",
"UGC" => "Cysteine",
"UGG" => "Tryptophan",
"UAA" => "STOP",
"UAG" => "STOP",
"UGA" => "STOP",
_ => throw new Exception("Invalid sequence")
};
[Benchmark]
public string[] ProteinsYieldSwitch() =>
strand.Chunked(3).Select(ToProtein).TakeWhile(protein => protein != "STOP").ToArray();
}
static class Program
{
public static void Main()
{
var summary = BenchmarkRunner.Run<BenchMe>();
}
public static IEnumerable<string> Chunked(this string input, int size)
{
for (var i = 0; i < input.Length; i += size)
yield return input[i..(i + size)];
}
}