Advertisement
NPSF3000

Simple Bool Speed Test

Aug 12th, 2011
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.20 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Diagnostics;
  6.  
  7. namespace SimpleBoolSpeedTest
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             while (true)
  14.             {
  15.                 //Extremely simplistic if (boolean)  speed test.
  16.                 long testLength = 100000000;
  17.  
  18.                 bool[] data = new bool[testLength];
  19.  
  20.                 var rand = new Random();
  21.  
  22.                 for (int i = 0; i < data.Length; i++)
  23.                     data[i] = (rand.Next(2) > 0);
  24.  
  25.                 var sw = new Stopwatch();
  26.                 long count = 0;
  27.  
  28.                 sw.Start();
  29.  
  30.                 for (int i = 0; i < data.Length; i++)
  31.                     if (data[i]) count++;
  32.  
  33.                 sw.Stop();
  34.  
  35.                 Console.WriteLine("Simple 'if (boolean)' speed test:" + count.ToString());
  36.  
  37.                 Console.WriteLine(testLength.ToString("N0") + " iterations in " + sw.ElapsedMilliseconds.ToString("N0") + "ms");
  38.                 Console.WriteLine((testLength / sw.ElapsedMilliseconds).ToString("N0") + " its/ms");
  39.                 Console.ReadLine();
  40.             }
  41.         }
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement