I've the following code:
int n = 150000;
int[] myArray = Enumerable.Range(1, n).ToArray();
So I want myArray contains an enumeration like 1,2,3,4,etc...
Of course the size of the array should be variable.
The thing is this is called millions of times inside a Parallel.For so I'm looking for a way to improve it as fast as it possible. Every iteration, n is different.
I just nugged the CommunityToolkit.HighPerformance in order to use some advantages from there, I'm wonder if I can use Span<T> to replace the above code, since I read this code:
var array = new byte[100];
var span = new Span<byte>(array);
span.Fill(255);
So I tried to do this:
var myArray = new int[n];
var span = new Span<int>(myArray );
span.Fill(/*nothing works here */);
So how can I populate that array with a serie of 1 to n?
I will accept another way instead using Fill or even Span<T>. The objective is made faster the whole process.
Here is a vectorized implementation of a
FillIncrementalmethod. TheVector<T>is a small container of values that a single CPU core can process in parallel. In my PC theVector.IsHardwareAcceleratedistrueand theVector<int>.Countis8. Initially aVector<int>is filled with the values from 1 to 8. Then in each step all these values are incremented by 8 with a single+=operation, and the incremented vector is copied to the next section of the target array. Finally the last few slots in the array that have not been filled by the vector (because the arrayLengthmight not be divisible by 8), are filled with a simpleforloop:Usage example:
In my PC the
FillIncrementalmethod is about 4 times faster than filling the array with a simpleforloop (online benchmark).I am not overly familiar with vectors, so it might be possible to optimize further the above approach.
Update: Enigmativity mentioned in the comments that the simple
int[] myArray = Enumerable.Range(1, n).ToArray()is actually faster than the above vectorized implementation. My own benchmark confirms this observation. Currently I have no idea why theEnumerable.Rangeis so fast. According to the source code is should perform similarly to theFillSimpleabove, so it should be around 3 times slower (taking into account the constant time of instantiating the array). It's around 15% faster instead (on .NET 7, .NET 6 and .NET 5).