What does Array.Reverse () do to compile?

I would like to know that Array.Reverse() in C # will compile and what optimizations will be made.

My research led me to several ways, some of which looked like the XOR method:

 Reverse(Array) { for(int i = 0, int len = Array.Length; i < Array.Length, i++ len--) { Array[i] ^= Array[len]; Array[len] ^= Array[i]; Array[i] ^= Array[len]; } } 

What I found very capable of working with smaller arrays. As they get larger, although performance starts to deteriorate, however, it seems to have better memory applications due to the nature of the inverse process in memory.

The second most noticeable way to access the array is to use a temporary array, which I will not write, since it is quite straightforward. But basically set the initial array of the first element to the last element of the temp array, etc. This method tends to be the fastest method when memory is not a problem.

So my question is, does Array.Reverse() specific method? If not, how to determine which method to use? Which really comes down to whether I need to trust the system libraries and the compiler to decide which is the fastest solution, and exactly how much I should trust this solution.

+4
source share
1 answer

Array.Reverse is currently implemented in the .NET environment using a temporary array. You can find this using ILSpy. The compiler does not say this. The method is implemented in a certain way, there is nothing compiler.

Regardless of whether this is the optimal solution, it depends on what you think is optimal, so you need to determine this for yourself, and then attach the profiler and check it.

+4
source

All Articles