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

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

Open
Smurf-IV opened this issue Jul 8, 2019 · 3 comments
Open

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

Smurf-IV opened this issue Jul 8, 2019 · 3 comments

Comments

@Smurf-IV
Copy link

Smurf-IV commented Jul 8, 2019

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;
                }
            }
        }
    }
@jackmott
Copy link
Owner

jackmott commented Jul 8, 2019

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.

@Smurf-IV
Copy link
Author

Smurf-IV commented Jul 8, 2019

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

@jackmott
Copy link
Owner

jackmott commented Jul 8, 2019

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.

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