C
C#2mo ago
Faker

✅ Can we find min and max values of any given array directly? (use of any built-in methods?)

Hi guys, I wanted to find min and max value of an array of numbers. I can do it using a loop, comparing each index. I have to manually set up everything and the algorithm is not as much efficient I think. So I was wondering if there was any in-built method or any better algorithm in terms of efficiency please
13 Replies
mg
mg2mo ago
There's Enumerable.Max() from LINQ, but under the hood it does the same thing You can't know what the min/max are without checking every element There are SIMD accelerated solutions that can check multiple pairs of entries simultaneously, but that's not something you need to worry about at this point
Faker
FakerOP2mo ago
oki noted, thanks !
mg
mg2mo ago
sure thing
jcotton42
jcotton422mo ago
>algorithm is not much efficient Surely it’s O(n)? How did you do it @Faker?
MODiX
MODiX2mo ago
Cattywampus
REPL Result: Success
static void FindMax(int[] arr)
{
ref var tref = ref MemoryMarshal.GetArrayDataReference(arr);
var min = Vector128<int>.Zero;

for(int i = 0; i < arr.Length; i+=4)
{
var val = Vector128.LoadUnsafe(ref Unsafe.Add(ref tref, i));
min = Vector128.Max(min, val);
}

var max = min.ToScalar();
max = Math.Max(max, min.GetElement(1));
max = Math.Max(max, min.GetElement(2));
max = Math.Max(max, min.GetElement(3));

Console.WriteLine(max);
}

FindMax(new int[]{1,2,3,4,5,6,7,8,9,0,0, 12});
static void FindMax(int[] arr)
{
ref var tref = ref MemoryMarshal.GetArrayDataReference(arr);
var min = Vector128<int>.Zero;

for(int i = 0; i < arr.Length; i+=4)
{
var val = Vector128.LoadUnsafe(ref Unsafe.Add(ref tref, i));
min = Vector128.Max(min, val);
}

var max = min.ToScalar();
max = Math.Max(max, min.GetElement(1));
max = Math.Max(max, min.GetElement(2));
max = Math.Max(max, min.GetElement(3));

Console.WriteLine(max);
}

FindMax(new int[]{1,2,3,4,5,6,7,8,9,0,0, 12});
Console Output
12
12
Compile: 548.824ms | Execution: 64.503ms | React with ❌ to remove this embed.
Cattywampus
Cattywampus2mo ago
like that ☝️ it might not be faster due to GetElement()s, should be easily be tackled with shuffles then use myVec.ToScalar() which is free compared to GetElement tbfh, 100% sure Linq already be doing this under the hood
mg
mg2mo ago
surprisingly no at least not the file i checked oh wait, yes it does, i just can't read code properly
Cattywampus
Cattywampus2mo ago
yeah, min/max is like very easy to vectorized
Faker
FakerOP2mo ago
euh I iterate through entire array, take the maximum as being the first element, then check for each subsequent element, time complexity of O(n) yeah what is the meaning of vectorized please ;c... I once saw the syntax of C++ where the term "vector" was used
mg
mg2mo ago
SIMD-accelerated types in .NET - .NET
This article describes SIMD-enable types in .NET and demonstrates how to use hardware SIMD operations in C# and .NET.
mg
mg2mo ago
it's a somewhat advanced topic not the same vectors in C++, those are equivalent to C#'s lists https://www.meziantou.net/finding-maximum-value-in-an-array-using-vectorization.htm here's an example of finding max using it
Faker
FakerOP2mo ago
oh ok, I will just scheme through, just to see what it is all about Thanks !
mg
mg2mo ago
sure thing

Did you find this page helpful?