static void Main(string[] args) { // Run each method once to make sure they are all compiled Program.TestBitArray(1, 100); Program.TestBooleanArray(1, 100); Program.TestByteArray(1, 100); Program.TestIntArray(1, 100); Program.TestLongArray(1, 100); // Get the test values Console.Write("Iterations? "); var iterations = int.Parse(Console.ReadLine()); Console.Write("Length? "); var length = int.Parse(Console.ReadLine()); Console.WriteLine($"Performing tests with {iterations} iterations and length {length}."); // Test the boolean array Program.TestHarness("An array of bools", iterations, length, Program.TestBooleanArray); // Test System.Collection.BitArray Program.TestHarness("System.Collections.BitArray", iterations, length, Program.TestBitArray); // Test custom binary implementations Program.TestHarness("byte[]", iterations, length, Program.TestByteArray); Program.TestHarness("uint[]", iterations, length, Program.TestIntArray); Program.TestHarness("ulong[]", iterations, length, Program.TestLongArray); Console.ReadLine(); } /// /// The test harness. /// /// The name of the test. /// The number of iterations. /// The length of the bit array. /// The test action. private static void TestHarness(string testName, int iterations, int length, Action testAction) { var watch = Stopwatch.StartNew(); testAction(iterations, length); watch.Stop(); Console.WriteLine($"Finished test: {testName} -> {watch.ElapsedMilliseconds}ms"); } /// /// Under the hood, just an array of bools. /// private static void TestBooleanArray(int iterations, int length) { var bools = new BitBufferBools(length); for(var i = 0; i < iterations; i++) { for (var l = 0; l < length; l++) { bools.Set(l, !bools.Get(length - l - 1)); } } } /// /// Under the hood, a System.Collections.BitArray /// private static void TestBitArray(int iterations, int length) { var bits = new BitArray(length); for (var i = 0; i < iterations; i++) { for (var l = 0; l < length; l++) { bits.Set(l, !bits.Get(length - l - 1)); } } } /// /// Under the hood, a byte[] /// private static void TestByteArray(int iterations, int length) { var bits = new ByteBitBuffer(length); for (var i = 0; i < iterations; i++) { for (var l = 0; l < length; l++) { bits.Set(l, !bits.Get(length - l - 1)); } } } /// /// Under the hood, a uint[] /// private static void TestIntArray(int iterations, int length) { var bits = new IntBitBuffer(length); for (var i = 0; i < iterations; i++) { for (var l = 0; l < length; l++) { bits.Set(l, !bits.Get(length - l - 1)); } } } /// /// Under the hood, a ulong[] /// private static void TestLongArray(int iterations, int length) { var bits = new LongBitBuffer(length); for (var i = 0; i < iterations; i++) { for (var l = 0; l < length; l++) { bits.Set(l, !bits.Get(length - l - 1)); } } }