- tst_src.zip
- QuickGraph
- NGraphviz.dll
- NGraphviz.Helpers.dll
- QuickGraph.Algorithms.dll
- QuickGraph.Algorithms.Graphviz.dll
- QuickGraph.Applications.dll
- QuickGraph.Collections.dll
- QuickGraph.Concepts.dll
- QuickGraph.dll
- QuickGraph.Exceptions.dll
- QuickGraph.Predicates.dll
- QuickGraph.Processors.dll
- QuickGraph.Representations.dll
- QuickGraph.Serialization.dll
- TernarySearchTree
- Tst.Benchmark
- Tst.Tests
- Tst.Utility
- Tst
- TstConsole
- TstUnitTests
- tst_demo.zip
- Release
- Tst.dll
- Tst_doc.chm
|
// Ternary Search Tree Implementation for C#
//
// Copyright (c) 2004 Jonathan de Halleux
//
// This software is provided 'as-is', without any express or implied warranty.
//
// In no event will the authors be held liable for any damages arising from
// the use of this software.
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
// you must not claim that you wrote the original software.
// If you use this software in a product, an acknowledgment in the product
// documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such, and must
// not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source
// distribution.
//
// Ternary Search Tree Implementation for C# Library HomePage:
// http://www.dotnetwiki.org
// Author: Jonathan de Halleux
// Algorithm found in J. L. Bentley and R. Sedgewick,
// Fast algorithms for sorting and searching strings,
// in Proceedings of the Eighth Annual ACM-SIAM Symposium on Discrete Algorithms,
// New Orleans Louisiana, January 5-7,
// 1997
// created on 9/01/2004 at 15:41
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Xml;
using Win32;
namespace Tst.Benchmark
{
public class DictionaryBencher
{
private Hashtable hashtable;
private TstDictionary tstDictionary;
private Win32.HiPerfTimer timer;
private XmlDocument doc;
private XmlElement root;
private XmlElement testElement;
private StringCollection words;
public DictionaryBencher(int wordCount)
{
this.words = DictionaryProvider.LoadWords("../../words.txt",wordCount);
this.hashtable=null;
this.tstDictionary = null;
this.timer = new HiPerfTimer();
this.doc = new XmlDocument();
this.root = doc.CreateElement("benchmark");
this.doc.AppendChild(root);
this.testElement = null;
}
public StringCollection Words
{
get
{
return words;
}
}
public XmlDocument Results
{
get
{
return doc;
}
}
internal HiPerfTimer Timer
{
get
{
return timer;
}
}
public void Run()
{
AddTest(100);
AddTest(1000);
AddTest(10000);
AddTest(25000);
AddTest(75000);
AddTest(Words.Count);
ContainsTest(100);
ContainsTest(1000);
ContainsTest(10000);
ContainsTest(25000);
ContainsTest(75000);
ContainsTest(Words.Count);
}
public void StartTestXml(string name, int wordCount)
{
this.testElement = doc.CreateElement("tests");
this.root.AppendChild(testElement);
XmlAttribute at = doc.CreateAttribute("name");
at.Value = name;
testElement.Attributes.Append(at);
at = doc.CreateAttribute("word-count");
at.Value = wordCount.ToString();
testElement.Attributes.Append(at);
}
public void FinishTestXml()
{
this.testElement=null;
}
public void StoreTestResultXml(string name, double time)
{
XmlElement node = doc.CreateElement("run");
testElement.AppendChild(node);
XmlAttribute at = doc.CreateAttribute("name");
at.Value = name;
node.Attributes.Append(at);
at = doc.CreateAttribute("duration");
at.Value = time.ToString();
node.Attributes.Append(at);
}
public void AddTest(int n)
{
int i;
Console.WriteLine("Adding {0} elements", n);
this.StartTestXml("Addition",n);
Console.Write("Hashtable (ordered insertion): ");
hashtable = new Hashtable();
Timer.Start();
i=0;
foreach(string s in Words)
{
hashtable.Add(s,null);
++i;
if (i==n)
break;
}
Timer.Stop();
Console.WriteLine("{0} s",Timer.Duration);
hashtable = null;
GC.Collect();
this.StoreTestResultXml("Hashtable (ordered insertion)",Timer.Duration);
Console.Write("Hashtable (balanced insertion): ");
hashtable = new Hashtable();
Timer.Start();
i=0;
for (int j=n/2+1;j<n;++j)
{
hashtable.Add(Words[j],null);
hashtable.Add(Words[n-j],null);
++i;
if (i==n)
break;
}
Timer.Stop();
Console.WriteLine("{0} s",Timer.Duration);
hashtable = null;
GC.Collect();
this.StoreTestResultXml("Hashtable (balanced insertion)",Timer.Duration);
Console.Write("TstDictionary (ordered insertion): ");
tstDictionary = new TstDictionary();
Timer.Start();
i=0;
foreach(string s in Words)
{
tstDictionary.Add(s,null);
++i;
if (i==n)
break;
}
Timer.Stop();
Console.WriteLine("{0} s",Timer.Duration);
tstDictionary = null;
GC.Collect();
this.StoreTestResultXml("TstDictionary (ordered insertion)",Timer.Duration);
Console.Write("TstDictionary (balanced insertion): ");
tstDictionary = new TstDictionary();
Timer.Start();
i=0;
for (int j=n/2+1;j<n;++j)
{
tstDictionary.Add(Words[j],null);
tstDictionary.Add(Words[n-j],null);
++i;
if (i==n)
break;
}
Timer.Stop();
Console.WriteLine("{0} s",Timer.Duration);
tstDictionary = null;
GC.Collect();
this.StoreTestResultXml("TstDictionary (balanced insertion)",Timer.Duration);
this.FinishTestXml();
}
public void ContainsTest(int n)
{
int i;
Console.WriteLine("Searching {0} elements", n);
this.StartTestXml("Contains",n);
Console.Write("Hashtable (ordered insertion): ");
hashtable = new Hashtable();
i=0;
foreach(string s in Words)
{
hashtable.Add(s,null);
++i;
if (i==n)
break;
}
Timer.Start();
foreach(string s in Words)
{
hashtable.Contains(s);
++i;
if (i==n)
break;
}
Timer.Stop();
Console.WriteLine("{0} s",Timer.Duration);
hashtable = null;
GC.Collect();
this.StoreTestResultXml("Hashtable (ordered insertion)",Timer.Duration);
Console.Write("Hashtable (balanced insertion): ");
hashtable = new Hashtable();
i=0;
for (int j=n/2+1;j<n;++j)
{
hashtable.Add(Words[j],null);
hashtable.Add(Words[n-j],null);
++i;
if (i==n)
break;
}
Timer.Start();
foreach(string s in Words)
{
hashtable.Contains(s);
++i;
if (i==n)
break;
}
Timer.Stop();
Console.WriteLine("{0} s",Timer.Duration);
hashtable = null;
GC.Collect();
this.StoreTestResultXml("Hashtable (balanced insertion)",Timer.Duration);
Console.Write("TstDictionary (ordered insertion): ");
tstDictionary = new TstDictionary();
i=0;
foreach(string s in Words)
{
tstDictionary.Add(s,null);
++i;
if (i==n)
break;
}
Timer.Start();
foreach(string s in Words)
{
tstDictionary.Contains(s);
++i;
if (i==n)
break;
}
Timer.Stop();
Console.WriteLine("{0} s",Timer.Duration);
tstDictionary = null;
GC.Collect();
this.StoreTestResultXml("TstDictionary (ordered insertion)",Timer.Duration);
Console.Write("TstDictionary (balanced insertion): ");
tstDictionary = new TstDictionary();
i=0;
for (int j=n/2+1;j<n;++j)
{
tstDictionary.Add(Words[j],null);
tstDictionary.Add(Words[n-j],null);
++i;
if (i==n)
break;
}
Timer.Start();
foreach(string s in Words)
{
tstDictionary.Contains(s);
++i;
if (i==n)
break;
}
Timer.Stop();
Console.WriteLine("{0} s",Timer.Duration);
tstDictionary = null;
GC.Collect();
this.StoreTestResultXml("TstDictionary (balanced insertion)",Timer.Duration);
this.FinishTestXml();
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Jonathan de Halleux is Civil Engineer in Applied Mathematics. He finished his PhD in 2004 in the rainy country of Belgium. After 2 years in the Common Language Runtime (i.e. .net), he is now working at Microsoft Research on Pex (http://research.microsoft.com/pex).