Skip to content

Instantly share code, notes, and snippets.

@redknightlois
Last active August 29, 2015 14:26
Show Gist options
  • Save redknightlois/045d3022c2c28e1498e2 to your computer and use it in GitHub Desktop.
Save redknightlois/045d3022c2c28e1498e2 to your computer and use it in GitHub Desktop.
[Task(platform: BenchmarkPlatform.X64, jitVersion: BenchmarkJitVersion.LegacyJit)]
[Task(platform: BenchmarkPlatform.X64, jitVersion: BenchmarkJitVersion.RyuJit)]
public class Jit_BoolToInt
{
private bool first;
private bool second;
public Jit_BoolToInt()
{
first = true;
second = false;
}
[Benchmark]
public int Framework()
{
int sum = Convert.ToInt32(first);
sum += Convert.ToInt32(second);
return sum;
}
[Benchmark]
public int IfThenElse()
{
int sum = first ? 1 : 0;
sum += second ? 1 : 0;
return sum;
}
[Benchmark]
public int UnsafeConvert()
{
unsafe
{
bool v1 = first;
int sum = *(byte*)(&v1);
bool v2 = second;
sum += *(byte*)(&v2);
return sum;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment