jackmott / LinqFaster

Linq-like extension functions for Arrays, Span<T>, and List<T> that are faster and allocate less.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Enhancement] Please can you add a GetMinMax() style function ?

Smurf-IV opened this issue · comments

Something better than this ?

    public static void GetMinMax(double[] values, out double min, out double max)
    {
        min = double.NaN;
        max = double.NaN;
        int length = values.Length;
        if (length > 0)
        {
            min = values[0];
            max = min;
            for (int i = 1; i < length; i++)
            {
                double value = values[i];
                if (min > value)
                {
                    min = value;
                }
                else if (max < value)
                {
                    max = value;
                }
            }
        }
    }

You can't get much faster than that without a SIMD implementation. A small thing you can do is don't use length in the for loop, use values.Length, this will ensure bounds check elimination. And you could use a tuple for the return value rather than two out values (won't change performance just may be nicer to use)

If a SIMD version would be useful let me know, I could add that maybe.

A SIMD would be useful, and for completeness the above one (With the changes you have recommended) as well

feel free to submit a PR with the above one! the pain is that you have to implement it for every primitive type. So its easy just annoying.