private static Dictionary <string, double> RunTest(IDawgFactory factory) { IEnumerable <KeyValuePair <string, ushort> > pairs = GetWordNumberPairs(); var metrics = new Dictionary <string, double> (); IDawgBuilder dawgBuilder = null; var buildTime = Time(() => { dawgBuilder = factory.CreateBuilder(pairs); }); metrics.Add("Build Time, ms", buildTime); MemoryStream stream = null; var saveTime = Time(() => { stream = new MemoryStream(); dawgBuilder.Save(new NonDisposableStream(stream)); }); metrics.Add("Save Time, ms", saveTime); metrics.Add("File Size, bytes", stream.Position); stream.Position = 0; IDawg dawg = null; var before = GC.GetTotalMemory(true); var loadTime = Time(() => { dawg = factory.Load(stream); }); var after = GC.GetTotalMemory(true); metrics.Add("Load Time, ms", loadTime); metrics.Add("RAM, bytes", after - before); const int nLookups = 1000000; var lookupTime = Time(() => { foreach (string word in RandomWords().Take(nLookups)) { if (dawg [word] == 0) { throw new Exception("dawg [" + word + "] == 0"); } } }); metrics.Add("Lookups Per Sec", nLookups * 1000.0 / lookupTime); var matchPrefixTime = Time(() => { #pragma warning disable 219 int x = 0; #pragma warning restore 219 foreach (var keyValuePair in RandomWords().SelectMany(w => dawg.MatchPrefix(w.Take(w.Length / 2))).Take(nLookups)) { x += keyValuePair.Value; } }); metrics.Add("Prefix Matches Per Sec", nLookups * 1000.0 / matchPrefixTime); return(metrics); }
public IEnumerable <KeyValuePair <string, TPayload> > MatchPrefix(IEnumerable <char> prefix) { return(dawg.MatchPrefix(prefix)); }