PC Review


Reply
Thread Tools Rate Thread

Buffer.BlockCopy vs Array.Copy

 
 
Jono
Guest
Posts: n/a
 
      26th Mar 2009
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
 
Reply With Quote
 
 
 
 
miher
Guest
Posts: n/a
 
      26th Mar 2009


"Jono" <(E-Mail Removed)> wrote in message
news:de036da2-1fb4-459b-aef7-(E-Mail Removed)...
> 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

 
Reply With Quote
 
Jono
Guest
Posts: n/a
 
      26th Mar 2009
On 26 Mar, 11:35, "miher" <feher.zs...@gmail.com> wrote:
> "Jono" <jono.p...@gmail.com> wrote in message
>
> news:de036da2-1fb4-459b-aef7-(E-Mail Removed)...
>
>
>
> > 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


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

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
The Copy Buffer & RichTextBox Tom Microsoft C# .NET 0 26th Feb 2008 08:06 AM
Converting an array to a buffer =?Utf-8?B?RGFtaXIgRGV6ZWxqaW4=?= Microsoft VC .NET 1 27th Oct 2006 07:51 AM
'copy to buffer' keys ? - Bobb - Windows Vista General Discussion 10 8th Jul 2006 12:32 AM
Copying data from unmanaged buffer to local array David Smith Microsoft C# .NET 0 1st Dec 2004 02:33 PM
BlockCopy Urs Wigger Microsoft C# .NET 2 19th Nov 2003 08:11 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:41 PM.