Skip to content
Permalink
Browse files

Add FindTrie benchmark

  • Loading branch information...
bobbymcr committed Apr 6, 2019
1 parent b2c08c9 commit ac6526342882916681c1e162f42ed5831f6da94e
@@ -0,0 +1,89 @@
// <copyright file="FindTrie.cs" company="Brian Rogers">
// Copyright (c) Brian Rogers. All rights reserved.
// </copyright>

namespace Words.Benchmark
{
using System;
using BenchmarkDotNet.Attributes;

[CoreJob]
public class FindTrie : LoadTrieBase
{
private const string WordStringMissing = "ZOATECHNICAL";
private const string WordString6 = "LOCKED";
private const string WordString12 = "ZOOTECHNICAL";

private static readonly Str WordStrMissing = Str.Parse(WordStringMissing);
private static readonly Str WordStr6 = Str.Parse(WordString6);
private static readonly Str WordStr12 = Str.Parse(WordString12);

private StringTrie stringTrie;
private StrTrie strTrie;

[Params(-1, 6, 12)]
public int Len { get; set; }

[GlobalSetup]
public void Setup()
{
WriteFiles();
this.stringTrie = LoadString(100);
this.strTrie = LoadStr(100);
}

[Benchmark]
public int StringF()
{
return Find(this.stringTrie, this.WordString(), 0);
}

[Benchmark]
public int StrF()
{
return Find(this.strTrie, this.WordStr());
}

private static int Find(StringTrie.INode node, string word, int index)
{
if (node == null)
{
return -1;
}

if (index == word.Length)
{
return node.Value.Length;
}

return Find(node[word[index]], word, index + 1);
}

private static int Find(StrTrie trie, Str word)
{
return (int)trie.Find(word);
}

private string WordString()
{
switch (this.Len)
{
case -1: return WordStringMissing;
case 6: return WordString6;
case 12: return WordString12;
default: throw new NotImplementedException();
}
}

private Str WordStr()
{
switch (this.Len)
{
case -1: return WordStrMissing;
case 6: return WordStr6;
case 12: return WordStr12;
default: throw new NotImplementedException();
}
}
}
}
@@ -4,64 +4,33 @@

namespace Words.Benchmark
{
using System.Collections.Generic;
using System.IO;
using BenchmarkDotNet.Attributes;

[CoreJob]
[MemoryDiagnoser]
public class LoadTrie
public class LoadTrie : LoadTrieBase
{
[Params(25, 50, 100)]
public int Pct { get; set; }

[GlobalSetup]
public void Setup()
{
string[] lines = File.ReadAllLines("words.txt");

List<string> lines25 = new List<string>();
List<string> lines50 = new List<string>();
List<string> lines100 = new List<string>();
for (int i = 0; i < lines.Length; ++i)
{
string line = lines[i];
int n = i % 16;
if (n < 4)
{
lines25.Add(line);
}

if (n < 8)
{
lines50.Add(line);
}

if (n < 16)
{
lines100.Add(line);
}
}

File.WriteAllLines(FileName(25), lines25);
File.WriteAllLines(FileName(50), lines50);
File.WriteAllLines(FileName(100), lines100);
WriteFiles();
}

[Benchmark]
public int String()
{
StringTrie trie = StringTrie.Load(File.OpenRead(FileName(this.Pct)));
StringTrie trie = LoadString(this.Pct);
return trie.Count;
}

[Benchmark]
public int Str()
{
StrTrie trie = StrTrie.Load(File.OpenRead(FileName(this.Pct)));
StrTrie trie = LoadStr(this.Pct);
return trie.Count;
}

private static string FileName(int pct) => $"words_{pct}.txt";
}
}
@@ -0,0 +1,54 @@
// <copyright file="LoadTrieBase.cs" company="Brian Rogers">
// Copyright (c) Brian Rogers. All rights reserved.
// </copyright>

namespace Words.Benchmark
{
using System.Collections.Generic;
using System.IO;

public abstract class LoadTrieBase
{
protected LoadTrieBase()
{
}

protected static void WriteFiles()
{
string[] lines = File.ReadAllLines("words.txt");

List<string> lines25 = new List<string>();
List<string> lines50 = new List<string>();
List<string> lines100 = new List<string>();
for (int i = 0; i < lines.Length; ++i)
{
string line = lines[i];
int n = i % 16;
if (n < 4)
{
lines25.Add(line);
}

if (n < 8)
{
lines50.Add(line);
}

if (n < 16)
{
lines100.Add(line);
}
}

File.WriteAllLines(FileName(25), lines25);
File.WriteAllLines(FileName(50), lines50);
File.WriteAllLines(FileName(100), lines100);
}

protected static StringTrie LoadString(int pct) => StringTrie.Load(File.OpenRead(FileName(pct)));

protected static StrTrie LoadStr(int pct) => StrTrie.Load(File.OpenRead(FileName(pct)));

private static string FileName(int pct) => $"words_{pct}.txt";
}
}
@@ -11,7 +11,8 @@ internal sealed class Program
private static void Main()
{
// BenchmarkRunner.Run<LoadTrie>();
BenchmarkRunner.Run<FindWords>();
// BenchmarkRunner.Run<FindWords>();
BenchmarkRunner.Run<FindTrie>();
}
}
}

0 comments on commit ac65263

Please sign in to comment.
You can’t perform that action at this time.