72 lines
1.9 KiB
C#
72 lines
1.9 KiB
C#
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<int> inputs = new HashSet<int>();
|
|
|
|
while (inputs.Count < this.inputs.Length)
|
|
inputs.Add(random.Next());
|
|
this.inputs = inputs.ToArray();
|
|
}
|
|
|
|
|
|
[Test]
|
|
public void Benchmark()
|
|
{
|
|
int iters = 10;
|
|
|
|
BTree<int> btree = new BTree<int>();
|
|
Meassure(String.Format("BTree({0})", inputs.Length), iters, ()=>{
|
|
foreach (int n in inputs)
|
|
btree.Add(n);
|
|
|
|
btree.Clear();
|
|
});
|
|
|
|
HashSet<int> hashSet = new HashSet<int>(1000000);
|
|
Meassure(String.Format("HashSet({0})", inputs.Length), iters, ()=>{
|
|
foreach (int n in inputs)
|
|
hashSet.Add(n);
|
|
|
|
hashSet.Clear();
|
|
});
|
|
|
|
Dictionary<int, int> dict = new Dictionary<int, int>();
|
|
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);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
}
|
|
} |