Skip to content

Instantly share code, notes, and snippets.

@breyed
Created May 19, 2014 12:10
Show Gist options
  • Save breyed/dfbf2af2e4667fd29acd to your computer and use it in GitHub Desktop.
Save breyed/dfbf2af2e4667fd29acd to your computer and use it in GitHub Desktop.
Why is checked arithmetic in .NET sometimes faster than unckecked?
// Code for http://stackoverflow.com/q/23736786/145173
using System;
using System.Diagnostics;
class Program
{
static void Main(string[] args)
{
var s = new Stopwatch();
s.Start();
int a = 0;
for (int i = 0; i < 100000000; i += 3) {
if (i == 1000)
i *= 2;
if (i % 35 == 0)
++a;
}
s.Stop();
Console.WriteLine(s.ElapsedMilliseconds);
Console.WriteLine(a);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment