Skip to content

Conversation

@pbolduc
Copy link
Contributor

@pbolduc pbolduc commented Feb 13, 2024

This change replaces the use of HttpStatusCode.OK.Equals(statusCode) in favor of using the equals operator directly. Using the equals operator is around 26 - 3000 times faster depending on the processor. I tested on a Raspberry Pi 4b and am AMD Ryzen Threadripper PRO 3955WX. Consider the following benchmark,

    public class Benchmarks
    {
        private HttpStatusCode _statusCode;

        [GlobalSetup]
        public void Setup()
        {
            Random random = new Random(420);

            var values = Enum.GetValues(typeof(HttpStatusCode)).Cast<HttpStatusCode>().ToArray();
            for (int i = 0; i < 10; i++)
            {
                random.Shuffle(values);
            }
        }

        [Benchmark]
        public bool EqualsMethod()
        {
            return _statusCode.Equals(HttpStatusCode.OK);
        }

        [Benchmark(Baseline = true)]
        public bool EqualsOperator()
        {
            return _statusCode == HttpStatusCode.OK;
        }
    }

The results on Raspberry Pi 4,

BenchmarkDotNet v0.13.12, Debian GNU/Linux 12 (bookworm)
Unknown processor
.NET SDK 8.0.101
  [Host]     : .NET 8.0.1 (8.0.123.58001), Arm64 RyuJIT AdvSIMD
  DefaultJob : .NET 8.0.1 (8.0.123.58001), Arm64 RyuJIT AdvSIMD


| Method         | Mean       | Error     | StdDev    | Median     | Ratio | RatioSD |
|--------------- |-----------:|----------:|----------:|-----------:|------:|--------:|
| EqualsMethod   | 73.6396 ns | 0.4825 ns | 0.4277 ns | 73.5708 ns |     ? |       ? |
| EqualsOperator |  0.0197 ns | 0.0185 ns | 0.0164 ns |  0.0248 ns |     ? |       ? |

and the results on AMD Ryzen Threadripper PRO 3955WX,

BenchmarkDotNet v0.13.12, Windows 11 (10.0.22631.3085/23H2/2023Update/SunValley3)
AMD Ryzen Threadripper PRO 3955WX 16-Cores, 1 CPU, 32 logical and 16 physical cores
.NET SDK 8.0.200-preview.23624.5
  [Host]     : .NET 8.0.1 (8.0.123.58001), X64 RyuJIT AVX2
  DefaultJob : .NET 8.0.1 (8.0.123.58001), X64 RyuJIT AVX2


| Method         | Mean      | Error     | StdDev    | Median    | Ratio | RatioSD |
|--------------- |----------:|----------:|----------:|----------:|------:|--------:|
| EqualsMethod   | 8.3323 ns | 0.1652 ns | 0.1545 ns | 8.2500 ns |     ? |       ? |
| EqualsOperator | 0.0027 ns | 0.0036 ns | 0.0034 ns | 0.0018 ns |     ? |       ? |

Copy link
Collaborator

@ebozduman ebozduman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

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.

2 participants