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

Optimize CheckNumericOnly #190

Merged
merged 1 commit into from
Feb 4, 2024

Conversation

Saibamen
Copy link
Contributor

@Saibamen Saibamen commented Feb 3, 2024

Benchmark:


BenchmarkDotNet v0.13.12, Windows 10 (10.0.19045.3996/22H2/2022Update)
Intel Core i7-7820HQ CPU 2.90GHz (Kaby Lake), 1 CPU, 8 logical and 4 physical cores
.NET SDK 8.0.101
  [Host]               : .NET 8.0.1 (8.0.123.58001), X64 RyuJIT AVX2
  .NET 6.0             : .NET 6.0.26 (6.0.2623.60508), X64 RyuJIT AVX2
  .NET 8.0             : .NET 8.0.1 (8.0.123.58001), X64 RyuJIT AVX2
  .NET Framework 4.8.1 : .NET Framework 4.8.1 (4.8.9195.0), X64 RyuJIT VectorSize=256


Method Runtime Data Mean Error StdDev Ratio RatioSD Allocated Alloc Ratio
For .NET 6.0 1633782928459 13.825 ns 0.1971 ns 0.1747 ns baseline **** - NA
Foreach .NET 6.0 1633782928459 9.508 ns 0.2147 ns 0.2205 ns 1.45x faster 0.04x - NA
For .NET 8.0 1633782928459 8.053 ns 0.1646 ns 0.1459 ns baseline - NA
Foreach .NET 8.0 1633782928459 6.731 ns 0.1699 ns 0.3149 ns 1.19x faster 0.07x - NA
For .NET Framework 4.8.1 1633782928459 12.746 ns 0.2686 ns 0.2381 ns baseline - NA
Foreach .NET Framework 4.8.1 1633782928459 9.324 ns 0.2230 ns 0.3338 ns 1.36x faster 0.07x - NA
For .NET 6.0 NoNumericHere 13.085 ns 0.2023 ns 0.1793 ns baseline **** - NA
Foreach .NET 6.0 NoNumericHere 9.949 ns 0.2733 ns 0.7885 ns 1.37x faster 0.12x - NA
For .NET 8.0 NoNumericHere 8.901 ns 0.2812 ns 0.8157 ns baseline - NA
Foreach .NET 8.0 NoNumericHere 7.729 ns 0.2350 ns 0.6856 ns 1.16x faster 0.13x - NA
For .NET Framework 4.8.1 NoNumericHere 12.945 ns 0.1764 ns 0.1563 ns baseline - NA
Foreach .NET Framework 4.8.1 NoNumericHere 9.813 ns 0.2633 ns 0.7682 ns 1.36x faster 0.08x - NA
For .NET 6.0 Numeric56Text 14.553 ns 0.3292 ns 0.8900 ns baseline **** - NA
Foreach .NET 6.0 Numeric56Text 9.687 ns 0.2278 ns 0.2961 ns 1.52x faster 0.09x - NA
For .NET 8.0 Numeric56Text 8.759 ns 0.2091 ns 0.2719 ns baseline - NA
Foreach .NET 8.0 Numeric56Text 7.009 ns 0.1694 ns 0.1883 ns 1.25x faster 0.05x - NA
For .NET Framework 4.8.1 Numeric56Text 13.821 ns 0.3142 ns 0.5249 ns baseline - NA
Foreach .NET Framework 4.8.1 Numeric56Text 9.750 ns 0.2284 ns 0.4346 ns 1.42x faster 0.09x - NA
Code
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Jobs;

namespace BenchmarkTemplate;

[MemoryDiagnoser]
[Config(typeof(StyleConfig))]
[HideColumns("Job")]
[SimpleJob(RuntimeMoniker.Net481)]
[SimpleJob(RuntimeMoniker.Net60)]
[SimpleJob(RuntimeMoniker.Net80)]
public class CheckNumericOnlyBenchmarks
{
    [Params("Numeric56Text", "NoNumericHere", "1633782928459")]
    public string Data;

    [Benchmark(Baseline = true)]
    public bool For()
    {
        char c;
        for (int i = 0; i < Data.Length; i++)
        {
            c = Data[i];
            if (c < '0' && c > '9')
            {
                return false;
            }
        }

        return true;
    }

    [Benchmark]
    public bool Foreach()
    {
        foreach (var c in Data)
        {
            if (c < '0' && c > '9')
            {
                return false;
            }
        }

        return true;
    }
}

@barnhill
Copy link
Owner

barnhill commented Feb 4, 2024

Nice!!!

@barnhill barnhill merged commit aed199d into barnhill:master Feb 4, 2024
4 checks passed
@Saibamen Saibamen deleted the optimize_CheckNumericOnly branch February 4, 2024 12:24
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