using System; using System.Collections.Generic; using System.Linq; using System.Runtime.CompilerServices; using NUnit.Framework; namespace ln.collections.test { public class BenchmarkBTree { int[] inputs = new int[100000]; [SetUp] public void Setup() { Random random = new Random(); HashSet inputs = new HashSet(); while (inputs.Count < this.inputs.Length) inputs.Add(random.Next()); this.inputs = inputs.ToArray(); } [Test] public void Benchmark() { int iters = 10; BTree btree = new BTree(); Meassure(String.Format("BTree({0})", inputs.Length), iters, ()=>{ foreach (int n in inputs) btree.Add(n); btree.Clear(); }); HashSet hashSet = new HashSet(1000000); Meassure(String.Format("HashSet({0})", inputs.Length), iters, ()=>{ foreach (int n in inputs) hashSet.Add(n); hashSet.Clear(); }); Dictionary dict = new Dictionary(); Meassure(String.Format("Dictionary({0})", inputs.Length), iters, ()=>{ foreach (int n in inputs) dict.Add(n, n); dict.Clear(); }); } public void Meassure(string name, int iterations, Action action) { TestContext.Error.WriteLine("------ NEASSURE -------------------"); while (iterations-->0) { DateTime start = DateTime.Now; action(); DateTime stop = DateTime.Now; TestContext.Error.WriteLine("Meassure: {0}: {1}ms", name, (stop - start).TotalMilliseconds); } } } }