1

As you know, Array.Copy is faster than manually copying the contents of an array to another, because it does a straight memory copy from one location to another.

But let's say I have a short[] and a char[]. You can't do an Array.Copy between them because they're not of the same type, but the elements in them are the same size, i.e. sizeof(short) == sizeof(char). How do I circumvent the type limitation to do a memcpy from the short[] to the char[], without doing a for loop? I'm talking about something that would be the logical equivalent of reinterpret_cast<> in C++.

TL;DR: If two arrays have elements of the same size, can you manipulate that to copy elements between them faster? You could always have a for loop, but I'm looking for something more like a straight memcpy from one array to the other.


Also, I'd like to note one more time that for loops will not be accepted. I am well aware of them, but this is a performance question asking if there is a more efficient way to do this.

6
  • 1
    Have you tried Buffer.BlockCopy? Commented Aug 7, 2015 at 21:29
  • 1
    May want to refer: stackoverflow.com/a/3783189 Commented Aug 7, 2015 at 21:31
  • 1
    @KellyEthridge && nawfal: Well, I guess you guys just answered my question... Commented Aug 7, 2015 at 21:32
  • If your question is answered, please either post an answer or ask the commenters who answered to post an answer, so that the question is actually shown as having an answer. Commented Aug 7, 2015 at 23:54
  • 1
    IMHO, you should worry less about the rep and more about the integrity of the site. Leaving present and unanswered a question for which you no longer seek an answer leads others to potentially waste time checking your question so that they can help, or makes it more difficult for others with a similar question to find the actual answer (since it's hidden in the comments instead of a well-marked answer). Note that if you answer the question and someone else finds it and up-votes it, you stand to gain more than the +2 you are considering at the moment. Commented Aug 8, 2015 at 1:28

1 Answer 1

1

You can use Buffer.BlockCopy. Unlike Array.Copy, it copies the underlying bytes of the array to the destination. So for example, a short is made up of 2 bytes. An empty short[] of 5 might look like this:

                 1 short (0x0000)
                        ˅˅˅˅
    0000 0000 0000 0000 0000
         ^^          ^^
    1 byte (0x00)  Byte #8 (byte index 7)

Meanwhile, let's say we have a byte[], this time with 10 indices. It'll look something like this:

                             Last byte [9]
                                   ˅˅
        AF C0 24 19 05 67 F9 D7 24 B3
        ^^
    First byte [0]

Now, notice that these arrays are the exact same size: the short[] is 5 x 2 bytes, while the byte[] is 10 x 1. So if we ran a Buffer.BlockCopy(bytes, 0, shorts, 0, bytes.Length), which essentially copies the bytes over from bytes to shorts, the shorts would become:

AFC0 2419 0567 F9D7 24B3

So yeah, that's essentially what Buffer.BlockCopy does- it copies the underlying bytes of the array.


Some tips:

  • Use the array with smaller-sized elements for getting the count. For example, if I had used shorts.Length as the last parameter in the example above, I would have had to multiply it by sizeof(short) or it would only copy half the array.

  • BlockCopy doesn't work with primitives.

Sign up to request clarification or add additional context in comments.

Comments

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.