Buffer.BlockCopy vs Array.Copy

J

Jono

Hi Everyone,
I've read the documentation on MSDN regarding Buffer's methods being
faster than Array's for primitive types. So I gave it a try, but it
doesn't copy all the data I ask it to (it stops after 7 items). I have
a C# code sample, where I create an array with 32k items, and set the
value of each item to its index in the array... then I use both
Buffer.BlockCopy and Array.Copy to copy the first 25 items into a sub
array:


<code>
int[] source = new int[short.MaxValue];
for (int index = 0; index < source.Length; index++)
{
source[index] = index;
}
int count = 25;

int[] bufferBlockCopy = new int[count];
Buffer.BlockCopy(source, 0, bufferBlockCopy, 0,
count);
Print(bufferBlockCopy);

int[] arrayCopy = new int[count];
Array.Copy(source, 0, arrayCopy, 0, count);
Print(arrayCopy);
</code>

bufferBlockCopy has zeros after index 7, while arrayCopy has all the
correct indexes.

Have I fundamentally misunderstood the usage pattern of
Buffer.BlockCopy or is there some kind of bug in the framework (or my
test) code?

Many thanks,

Jono
 
M

miher

Jono said:
Hi Everyone,
I've read the documentation on MSDN regarding Buffer's methods being
faster than Array's for primitive types. So I gave it a try, but it
doesn't copy all the data I ask it to (it stops after 7 items). I have
a C# code sample, where I create an array with 32k items, and set the
value of each item to its index in the array... then I use both
Buffer.BlockCopy and Array.Copy to copy the first 25 items into a sub
array:


<code>
int[] source = new int[short.MaxValue];
for (int index = 0; index < source.Length; index++)
{
source[index] = index;
}
int count = 25;

int[] bufferBlockCopy = new int[count];
Buffer.BlockCopy(source, 0, bufferBlockCopy, 0,
count);
Print(bufferBlockCopy);

int[] arrayCopy = new int[count];
Array.Copy(source, 0, arrayCopy, 0, count);
Print(arrayCopy);
</code>

bufferBlockCopy has zeros after index 7, while arrayCopy has all the
correct indexes.

Have I fundamentally misunderstood the usage pattern of
Buffer.BlockCopy or is there some kind of bug in the framework (or my
test) code?

Many thanks,

Jono

Hi,

BlockCopy expects the number of bytes to copy, not the number of elements.

-Zsolt
 
J

Jono

Hi Everyone,
I've read the documentation on MSDN regarding Buffer's methods being
faster than Array's for primitive types. So I gave it a try, but it
doesn't copy all the data I ask it to (it stops after 7 items). I have
a C# code sample, where I create an array with 32k items, and set the
value of each item to its index in the array... then I use both
Buffer.BlockCopy and Array.Copy to copy the first 25 items into a sub
array:
<code>
               int[] source = new int[short.MaxValue];
               for (int index = 0; index < source.Length; index++)
               {
                   source[index] = index;
               }
               int count = 25;
               int[] bufferBlockCopy = new int[count];
               Buffer.BlockCopy(source, 0, bufferBlockCopy, 0,
count);
               Print(bufferBlockCopy);
               int[] arrayCopy = new int[count];
               Array.Copy(source, 0, arrayCopy, 0, count);
               Print(arrayCopy);
</code>
bufferBlockCopy has zeros after index 7, while arrayCopy has all the
correct indexes.
Have I fundamentally misunderstood the usage pattern of
Buffer.BlockCopy or is there some kind of bug in the framework (or my
test) code?
Many thanks,

Hi,

BlockCopy expects the number of bytes to copy, not the number of elements..

-Zsolt

Haha! Brilliant. Thank you for letting me know so quickly. Regards,
Jono
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top