Advertisement
NPSF3000

BitVector

Aug 12th, 2011
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.46 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Collections.Specialized;
  6. using System.Diagnostics;
  7.  
  8. namespace BitVector
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             BitVector32[] data = new BitVector32[32];
  15.  
  16.             var rand = new Random();
  17.  
  18.             //populate with random data;
  19.             for (int y = 0; y < 32; y++)
  20.                 for (int x = 0; x < 32; x++)
  21.                     data[y][1 << x] = rand.Next(2) == 0;
  22.  
  23.             //benchmark
  24.             while (true)
  25.             {
  26.                 int iterations = 10000;
  27.                 int count = 0;
  28.                 var sw = new Stopwatch();
  29.  
  30.                 sw.Start();
  31.                 for (int i = 0; i < iterations; i++)
  32.                     for (int y = 0; y < 32; y++)
  33.                         for (int x = 0; x < 32; x++)
  34.                             if (data[y][1 << x]) count++;
  35.                 sw.Stop();
  36.  
  37.                 iterations *= 32 * 32;
  38.  
  39.                 Console.WriteLine("Count: " + count.ToString("N0"));
  40.                 Console.WriteLine("Iterations: " + iterations.ToString("N0"));
  41.                 Console.WriteLine("Time: " + sw.ElapsedMilliseconds + "ms");
  42.                 Console.WriteLine("");
  43.                 Console.WriteLine((iterations / sw.ElapsedMilliseconds).ToString("N0") + " its/ms");
  44.                 Console.ReadLine();
  45.             }
  46.         }
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement