-
-
Notifications
You must be signed in to change notification settings - Fork 346
/
Program.cs
57 lines (51 loc) · 1.47 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
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using System.Linq;
[MemoryDiagnoser]
public class BenchMe
{
private readonly string word = "Thumbscrew-Japingly";
[Benchmark]
public bool Distinct()
{
var lowerLetters = word.ToLower().Where(char.IsLetter).ToList();
return lowerLetters.Distinct().Count() == lowerLetters.Count;
}
[Benchmark]
public bool GroupBy()
{
return word.ToLower().Where(Char.IsLetter).GroupBy(ltr => ltr).All(ltr_grp => ltr_grp.Count() == 1);
}
[Benchmark]
public bool Bitfield()
{
int letter_flags = 0;
foreach (char letter in word)
{
if (letter >= 'a' && letter <= 'z')
{
// shift 1 to the left for the letter's place in the alphabet
if ((letter_flags & (1 << (letter - 'a'))) != 0)
return false;
else
letter_flags |= (1 << (letter - 'a'));
}
else if (letter >= 'A' && letter <= 'Z')
{
// shift 1 to the left for the letter's place in the alphabet
if ((letter_flags & (1 << (letter - 'A'))) != 0)
return false;
else
letter_flags |= (1 << (letter - 'A'));
}
}
return true;
}
}
static class Program
{
public static void Main()
{
var summary = BenchmarkRunner.Run<BenchMe>();
}
}