using System.Collections.Generic;
using System.Diagnostics;
public static void Main(string[] args)
int seed = (int)DateTime.UtcNow.Ticks;
var st1 = new SpeedTest(delegate
t |= (Test)rand.Next(1, 9);
if (t.HasFlag(Test.Flag4))
var st2 = new SpeedTest(delegate
t |= (Test)rand.Next(1, 9);
if (HasFlag(t , Test.Flag4))
var st3 = new SpeedTest(delegate
var st4 = new SpeedTest(delegate
Console.WriteLine("Random to prevent optimizing out things {0}", num);
Console.WriteLine("HasFlag: {0}ms {1}ms {2}ms", st1.Min, st1.Average, st1.Max);
Console.WriteLine("Bitwise: {0}ms {1}ms {2}ms", st2.Min, st2.Average, st2.Max);
Console.WriteLine("Equality: {0}ms {1}ms {2}ms", st3.Min, st3.Average, st3.Max);
Console.WriteLine("Equality not found: {0}ms {1}ms {2}ms", st4.Min, st4.Average, st4.Max);
static bool HasFlag(Test flags, Test flag)
return (flags & flag) != 0;
public int Iterations { get; set; }
public int Times { get; set; }
public List<Stopwatch> Watches { get; set; }
public Action Function { get; set; }
public long Min { get { return Watches.Min(s => s.ElapsedMilliseconds); } }
public long Max { get { return Watches.Max(s => s.ElapsedMilliseconds); } }
public double Average { get { return Watches.Average(s => s.ElapsedMilliseconds); } }
public SpeedTest(Action func)
Watches = new List<Stopwatch>();
for (int i = 0; i < Times; i++)
var sw = Stopwatch.StartNew();
for (int o = 0; o < Iterations; o++)