Skip to content

Instantly share code, notes, and snippets.

@utterstep
Last active December 15, 2015 10:59
Show Gist options
  • Save utterstep/5249581 to your computer and use it in GitHub Desktop.
Save utterstep/5249581 to your computer and use it in GitHub Desktop.
C#: AND and modulo (%) operators comparsion
using System;
using System.Diagnostics;
namespace Tests
{
class Program
{
static void Main(string[] args)
{
Stopwatch s = new Stopwatch();
int nEven;
s.Start();
nEven = 0;
for (int i = 0; i < int.MaxValue; i++)
{
if (i % 2 == 0)
{
nEven++;
}
}
s.Stop();
Console.WriteLine("Modulo time:\t{0} ms", s.ElapsedMilliseconds);
s.Reset();
s.Start();
nEven = 0;
for (int i = 0; i < int.MaxValue; i++)
{
if ((i & 1) == 0)
{
nEven++;
}
}
s.Stop();
Console.WriteLine("AND time:\t{0} ms", s.ElapsedMilliseconds);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment