Last active
December 15, 2015 10:59
-
-
Save utterstep/5249581 to your computer and use it in GitHub Desktop.
C#: AND and modulo (%) operators comparsion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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