Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ignore case parameter for TryParseFast #76

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

dameng324
Copy link
Contributor

Enum.TryParse has two overloads which are:

public static bool TryParse<TEnum>(ReadOnlySpan<char> value, out TEnum result)
public static bool TryParse<TEnum>([NotNullWhen(true)] string? value, bool ignoreCase, out TEnum result)

so I add the ignoreCase overload.

Generated:

/// <summary>
/// Try parse a string to <see cref="global::UnitTests.UserTypeTest" /> value.
/// </summary>
/// <param name="states">The string representing a <see cref="global::UnitTests.UserTypeTest" /> value.</param>
/// <param name="ignoreCase">true to ignore case; false to consider case.</param>
/// <param name="result">The enum <see cref="global::UnitTests.UserTypeTest" /> parse result.</param>
/// <returns>True if the string is parsed successfully; otherwise, false.</returns>
public static bool TryParseFast(string states, bool ignoreCase, out UnitTests.UserTypeTest result)
{
    if (string.Equals(states, "Men", ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal))
    {
        result = UnitTests.UserTypeTest.Men;
        return true;
    }
    if (string.Equals(states, "Women", ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal))
    {
        result = UnitTests.UserTypeTest.Women;
        return true;
    }
    if (string.Equals(states, "None", ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal))
    {
        result = UnitTests.UserTypeTest.None;
        return true;
    }

    result = default;
    return false;
}

BenchMark Result

// * Summary *

BenchmarkDotNet v0.13.12, Windows 11 (10.0.22621.3296/22H2/2022Update/SunValley2)
AMD Ryzen 7 5800X, 1 CPU, 16 logical and 8 physical cores
.NET SDK 8.0.203
  [Host]     : .NET 8.0.3 (8.0.324.11423), X64 RyuJIT AVX2
  Job-SEFBGQ : .NET 8.0.3 (8.0.324.11423), X64 RyuJIT AVX2

Runtime=.NET 8.0  InvocationCount=64  IterationCount=32
IterationTime=120.0000 s  LaunchCount=1  WarmupCount=6

| Method                   | Mean         | Error       | StdDev      | Median       | Op/s          | Rank | Allocated |
|------------------------- |-------------:|------------:|------------:|-------------:|--------------:|-----:|----------:|
| FastToDisplay            |     1.451 ns |   0.2857 ns |   0.4098 ns |     1.562 ns | 689,230,769.2 |    1 |       6 B |
| FastGetLength            |     1.562 ns |   0.0000 ns |   0.0000 ns |     1.562 ns | 640,000,000.0 |    1 |       6 B |
| FastIsDefined            |     2.571 ns |   0.4976 ns |   0.7600 ns |     3.125 ns | 389,019,607.8 |    2 |       6 B |
| FastToDescription        |     2.894 ns |   0.4035 ns |   0.5656 ns |     3.125 ns | 345,600,000.0 |    2 |       6 B |
| FasterToString           |     3.027 ns |   0.5664 ns |   0.8819 ns |     2.344 ns | 330,322,580.6 |    2 |       6 B |
| FastTryParse             |     6.062 ns |   0.3882 ns |   0.5182 ns |     6.250 ns | 164,948,453.6 |    3 |       6 B |
| FastGetValues            |    10.104 ns |   0.9783 ns |   1.4642 ns |    10.156 ns |  98,969,072.2 |    4 |      46 B |
+| FastTryParseIgnoreCase   |    12.109 ns |   0.4804 ns |   0.6890 ns |    12.500 ns |  82,580,645.2 |    5 |       6 B |
| FastGetNames             |    15.904 ns |   1.1505 ns |   1.6500 ns |    15.625 ns |  62,877,193.0 |    6 |      54 B |
| NativeToString           |    96.562 ns |   4.3554 ns |   6.5190 ns |    96.875 ns |  10,355,987.1 |    7 |      36 B |
+| NativeTryParseIgnoreCase |   106.250 ns |   3.9949 ns |   5.6002 ns |   104.688 ns |   9,411,764.7 |    8 |      12 B |
| NativeTryParse           |   109.375 ns |   2.2716 ns |   3.1845 ns |   109.375 ns |   9,142,857.1 |    9 |      12 B |
| NativeGetLength          |   110.990 ns |   1.2101 ns |   1.8112 ns |   110.938 ns |   9,009,854.5 |   10 |      60 B |
| NativeGetNames           |   111.169 ns |   3.7058 ns |   5.1950 ns |   109.375 ns |   8,995,314.9 |   10 |      60 B |
| NativeIsDefined          |   118.103 ns |   3.4602 ns |   5.0720 ns |   118.750 ns |   8,467,153.3 |   11 |      36 B |
| NativeGetValues          |   157.701 ns |   2.1161 ns |   3.0348 ns |   156.250 ns |   6,341,118.2 |   12 |      50 B |
| NativeToDescription      | 5,427.121 ns | 163.6793 ns | 234.7440 ns | 5,398.438 ns |     184,259.8 |   13 |     752 B |
| NativeToDisplay          | 5,473.785 ns | 187.0457 ns | 262.2120 ns | 5,426.562 ns |     182,689.0 |   13 |     752 B |

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant