Created
June 2, 2024 19:45
-
-
Save gumbarros/44b56a67f8bf5cda7d9e48a01f6e4738 to your computer and use it in GitHub Desktop.
Benchmark of ways of checking enum flags
This file contains hidden or 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 BenchmarkDotNet.Attributes; | |
using BenchmarkDotNet.Mathematics; | |
namespace Benchmark; | |
/// <summary> | |
/// Options used for both parsing and evaluation of an expression. | |
/// </summary> | |
[Flags] | |
public enum ExpressionOptions | |
{ | |
/// <summary> | |
/// Specifies that no options are set. | |
/// </summary> | |
None = 1 << 0, | |
/// <summary> | |
/// Specifies case-insensitive matching. | |
/// </summary> | |
IgnoreCase = 1 << 1, | |
/// <summary> | |
/// No-cache mode. Ignores any pre-compiled expression in the cache. | |
/// </summary> | |
NoCache = 1 << 2, | |
/// <summary> | |
/// Treats parameters as arrays and returns a set of results. | |
/// </summary> | |
IterateParameters = 1 << 3, | |
/// <summary> | |
/// When using Round(), if a number is halfway between two others, it is rounded toward the nearest number that is away from zero. | |
/// </summary> | |
RoundAwayFromZero = 1 << 4, | |
/// <summary> | |
/// Specifies the use of CaseInsensitiveComparer for comparisons. | |
/// </summary> | |
[Obsolete("Please use CaseInsensitiveStringComparer")] | |
CaseInsensitiveComparer = 1 << 5, | |
/// <summary> | |
/// Specifies the use of CaseInsensitiveComparer for comparisons. | |
/// </summary> | |
#pragma warning disable CA1069 | |
CaseInsensitiveStringComparer = 1 << 5, | |
#pragma warning restore CA1069 | |
/// <summary> | |
/// Uses decimals instead of doubles as default floating point data type. | |
/// </summary> | |
DecimalAsDefault = 1 << 6, | |
/// <summary> | |
/// Defines a "null" parameter and allows comparison of values to null. | |
/// </summary> | |
AllowNullParameter = 1 << 7, | |
/// <summary> | |
/// Use ordinal culture on string compare | |
/// </summary> | |
OrdinalStringComparer = 1 << 8, | |
/// <summary> | |
/// Allow calculation with boolean values | |
/// </summary> | |
AllowBooleanCalculation = 1 << 9, | |
} | |
public static class ExpressionOptionsExtensions | |
{ | |
/// <summary> | |
/// Checks if the ExpressionOptions enum have an option selected. | |
/// </summary> | |
public static bool HasOption(this ExpressionOptions options, ExpressionOptions option) | |
{ | |
return (options & option) == option; | |
} | |
} | |
[RankColumn(NumeralSystem.Stars)] | |
[MemoryDiagnoser] | |
public class FlagsVsOptionsBenchmark | |
{ | |
private ExpressionOptions value = ExpressionOptions.AllowNullParameter | ExpressionOptions.CaseInsensitiveStringComparer; // Example value for benchmark | |
[Benchmark] | |
public bool HasFlag() | |
{ | |
return value.HasFlag(ExpressionOptions.CaseInsensitiveStringComparer); | |
} | |
[Benchmark] | |
public bool HasOption() | |
{ | |
return value.HasOption(ExpressionOptions.CaseInsensitiveStringComparer); | |
} | |
} | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var config = DefaultConfig.Instance | |
.WithOrderer(new DefaultOrderer(SummaryOrderPolicy.FastestToSlowest)); | |
var summary = BenchmarkRunner.Run<FlagsVsOptionsBenchmark>(config); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment