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

EgorBot for EgorBo in #107304 #78

Open
EgorBot opened this issue Sep 5, 2024 · 3 comments
Open

EgorBot for EgorBo in #107304 #78

EgorBot opened this issue Sep 5, 2024 · 3 comments

Comments

@EgorBot
Copy link
Owner

EgorBot commented Sep 5, 2024

Processing dotnet/runtime#107304 (comment) command:

Command

-awsamd -commit 72f9ee0d26c23b3c58ec5af8aeea316095761646 vs 767e4165641583c73918168a920a10513000572c -profiler --envvars DOTNET_JitDisasm:TrailingZeroCount_ulong

using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using System.Numerics;
using System.Runtime.CompilerServices;

public class Perf_BitOperations
{
    static T[] Array<T>(int count, int? seed = null)
    {
        var result = new T[count];
        var random = new Random(42);
        if (typeof(T) == typeof(byte) || typeof(T) == typeof(sbyte))
            random.NextBytes(Unsafe.As<byte[]>(result));
        else
            for (int i = 0; i < result.Length; i++)
                result[i] = GenerateValue<T>(random);
        return result;
    }

    static T GenerateValue<T>(Random random)
    {
        if (typeof(T) == typeof(uint))
            return (T)(object)(uint)random.Next();
        if (typeof(T) == typeof(ulong))
            return (T)(object)(ulong)random.Next();
        throw new NotImplementedException();
    }

    static uint[] input_uint = Array<uint>(1000);
    static ulong[] input_ulong = Array<ulong>(1000);

    [Benchmark]
    public int TrailingZeroCount_ulong()
    {
        int sum = 0;
        ulong[] input = input_ulong;
        for (int i = 0; i < input.Length; i++)
        {
            sum += BitOperations.TrailingZeroCount(input[i]);
        }
        return sum;
    }
}

(EgorBot will reply in this issue)

@EgorBot
Copy link
Owner Author

EgorBot commented Sep 5, 2024

Benchmark results on c7a_4xlarge

BenchmarkDotNet v0.14.0, Ubuntu 24.04 LTS (Noble Numbat)
AMD EPYC 9R14, 1 CPU, 16 logical and 16 physical cores
  Job-XAGHSR : .NET 9.0.0 (42.42.42.42424), X64 RyuJIT AVX-512F+CD+BW+DQ+VL+VBMI
  Job-FPFXDN : .NET 9.0.0 (42.42.42.42424), X64 RyuJIT AVX-512F+CD+BW+DQ+VL+VBMI
EnvironmentVariables=DOTNET_JitDisasm=TrailingZeroCount_ulong
Method Toolchain Mean Error Ratio
TrailingZeroCount_ulong Before 279.5 ns 0.10 ns 1.00
TrailingZeroCount_ulong After 278.0 ns 0.22 ns 0.99

BDN_Artifacts.zip

Flame graphs: Main vs PR 🔥
Hot asm: Main vs PR
Hot functions: Main vs PR
Counters: Main vs PR

For clean perf results, make sure you have just one [Benchmark] in your app.

@EgorBot
Copy link
Owner Author

EgorBot commented Sep 5, 2024

cc @EgorBo (logs)

@EgorBo
Copy link

EgorBo commented Sep 5, 2024

@EgorBot -awsamd -commit 72f9ee0d26c23b3c58ec5af8aeea316095761646 vs 767e4165641583c73918168a920a10513000572c -profiler --envvars DOTNET_JitDisasm:TrailingZeroCount_ulong

using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using System.Numerics;
using System.Runtime.CompilerServices;

public class Perf_BitOperations
{
    static T[] Array<T>(int count, int? seed = null)
    {
        var result = new T[count];
        var random = new Random(42);
        if (typeof(T) == typeof(byte) || typeof(T) == typeof(sbyte))
            random.NextBytes(Unsafe.As<byte[]>(result));
        else
            for (int i = 0; i < result.Length; i++)
                result[i] = GenerateValue<T>(random);
        return result;
    }

    static T GenerateValue<T>(Random random)
    {
        if (typeof(T) == typeof(uint))
            return (T)(object)(uint)random.Next();
        if (typeof(T) == typeof(ulong))
            return (T)(object)(ulong)random.Next();
        throw new NotImplementedException();
    }

    static uint[] input_uint = Array<uint>(1000);
    static ulong[] input_ulong = Array<ulong>(1000);

    [Benchmark]
    public int TrailingZeroCount_ulong()
    {
        int sum = 0;
        ulong[] input = input_ulong;
        for (int i = 0; i < input.Length; i++)
        {
            sum += BitOperations.TrailingZeroCount(input[i]);
        }
        return sum;
    }
}

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

No branches or pull requests

2 participants