2

Working on Windows OS and C# (means little endian) so no need for extra checks.

How can I increase speed of reversing an array of bytes using pointers?

Instead of a normal for loop:

const int  value = 133;
var bArr = new byte[]{ 0, 0, 0 , value };
int Len = bArr.Length;
var rrAb = new byt[Len];

for(int idx=0; idx<bArr.Length;idx++)
          rrAb[idx] = bArr[--Len];

String reverse using pointers

28
  • 1
    did you tried Array.Reverse? Commented Feb 26, 2016 at 22:03
  • @Gusman I want to do it not use it ... (; Commented Feb 26, 2016 at 22:04
  • 1
    Then a pointer will not speed up your code, accessing an array element is like using a pointer, same speed Commented Feb 26, 2016 at 22:08
  • Are you certain it can be made faster with pointers? .NET is a little weird with pointers, since everything is already a pointer underneath, so by declaring an explicit pointer into the heap-allocated array you can actually end up increasing the time (not to mention even worse than normal optimization because the compiler treats unsafe code as unoptimizable). If you want it faster, @Gusman is right on Array.Reverse, since it is implemented in low-level native C. Commented Feb 26, 2016 at 22:08
  • @Gusman extra method call i'll stick to for loop within same code block... i guess it's the best Commented Feb 26, 2016 at 22:12

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.