Skip to content

Benchmarks for #123027 (EgorBo) #586

@EgorBot

Description

@EgorBot

Processing dotnet/runtime#123027 (comment) command:

Command

-windows_intel -amd -intel --runtimes net8.0 net10.0

using BenchmarkDotNet.Attributes;
using System;


[MemoryDiagnoser]
public class Benchmarks
{
    const int N = 1000000;
    float[] _data;

    [GlobalSetup]
    public void Setup()
    {
        _data = new float[N];
        for (int i = 0; i < N; i++) _data[i] = 1;
    }

    [Benchmark]
    public double Iterate_With_Array()
    {
        double result = 0;

        for (int i = 0; i < N; i++)
        {
            result += _data[i];
        }

        return result;
    }

    [Benchmark]
    public double Iterate_With_Span()
    {
        double result = 0;
        Span<float> span = _data;

        for (int i = 0; i < N; i++)
        {
            result += span[i];
        }

        return result;
    }

    [Benchmark]
    public double Iterate_With_SpanWrapper()
    {
        double result = 0;
        SpanWrapper<float> span = new(_data);

        for (int i = 0; i < N; i++)
        {
            result += span[i];
        }

        return result;
    }

    [Benchmark]
    public double Iterate_With_ArrayWrapper()
    {
        double result = 0;
        ArrayWrapper<float> span = new(_data);

        for (int i = 0; i < N; i++)
        {
            result += span[i];
        }

        return result;
    }

    [Benchmark]
    public double Iterate_With_ArrayWrapperPoor()
    {
        double result = 0;
        ArrayWrapperPoor<float> span = new(_data);

        for (int i = 0; i < N; i++)
        {
            result += span[i];
        }

        return result;
    }
}

public readonly ref struct SpanWrapper<T>
{
    private readonly Span<T> _span;

    public SpanWrapper(Span<T> span)
    {
        _span = span;
    }

    public ref T this[int index]
        => ref _span[index];
}

public readonly ref struct ArrayWrapper<T>(T[] array)
{
    public ref T this[int index]
        => ref array[index];
}

public readonly ref struct ArrayWrapperPoor<T>(T[] array)
{
    public ref T this[int index]
        => ref array.AsSpan()[index];
}

(EgorBot will reply in this issue)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions