Created
May 12, 2020 20:16
-
-
Save lostmsu/ed31ec13815e2af674abd96d0a4fc8ae to your computer and use it in GitHub Desktop.
Comparing static readonly bool field to static property, set to a value computable at compile time
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
namespace Inline_vs_Static_Readonly | |
{ | |
using System; | |
using BenchmarkDotNet.Attributes; | |
using BenchmarkDotNet.Jobs; | |
using BenchmarkDotNet.Running; | |
[SimpleJob(RuntimeMoniker.Net48)] | |
[SimpleJob(RuntimeMoniker.Mono)] | |
[SimpleJob(RuntimeMoniker.NetCoreApp31)] | |
public class InlineOrStaticReadonly | |
{ | |
static void Main(string[] args) | |
{ | |
var summary = BenchmarkRunner.Run<InlineOrStaticReadonly>(); | |
} | |
static readonly bool is32bit = IntPtr.Size == 4; | |
[Benchmark] | |
public int StaticReadonly() { | |
int result = 0; | |
for(int i = 0; i < IntPtr.Size * 50_000; i++) { | |
if (is32bit) | |
result += IntPtr.Size; | |
} | |
return result; | |
} | |
static bool Is32Bit => IntPtr.Size == 4; | |
[Benchmark] | |
public int Inline() { | |
int result = 0; | |
for(int i = 0; i < IntPtr.Size * 50_000; i++) { | |
if (Is32Bit) | |
result += IntPtr.Size; | |
} | |
return result; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment