473,320 Members | 2,189 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Question about array copy performance.

Which one is faster or any other better way to do it.

I have an array of byte with name:
sendBuffer, and i will like to make some thing like that
the value started from index of the array in realDataSent is now copy
to the beginning of the sendBuffer.

I have tried:
Array.Copy(sendBuffer,
realDataSent, sendBuffer, 0, sendBuffer.Length - realDataSent);
and this one:
byte[] tempBuffer =
(byte[])sendBuffer.Clone();
Array.Clear(sendBuffer, 0,
sendBuffer.Length);
Array.Copy(tempBuffer,
realDataSent, sendBuffer, 0, tempBuffer.Length - realDataSent);

Which one is faster or any other better way to do it.

Thanks

Nov 9 '07 #1
8 7404
On 2007-11-08 19:36:23 -0800, li*********@yahoo.com.hk said:
[...]
realDataSent, sendBuffer, 0, sendBuffer.Length - realDataSent);
and this one:
byte[] tempBuffer =
(byte[])sendBuffer.Clone();
Array.Clear(sendBuffer, 0,
sendBuffer.Length);
Array.Copy(tempBuffer,
realDataSent, sendBuffer, 0, tempBuffer.Length - realDataSent);

Which one is faster or any other better way to do it.
Well, which one is faster when you try each and time it? That should
be your first step in the investigation, rather than asking in a
newsgroup.

That said, I suspect the first option would be faster, assuming
Array.Copy() is smart about dealing with overlapping arrays. There's
no reason it should need to create a temporary copy of the original
data, so it should be faster. The second option, as compared to a
straight copy, is clearly going to be slower, not even counting that
you include a useless call to Array.Clear().

If the implementation of Array.Copy() is brain-dead and always creates
a temporary copy of a source array that overlaps a destination array,
then I would still expect the first version to be faster. But only
because the second version wastes time with the Array.Clear() call.
Take that out, and the two would be equivalent in that case.

But I doubt the implementation of Array.Copy() is brain-dead. In fact,
I'd guess there's a pretty good chance it's been very well optimized.

Regardless, if you really need to care about the performance of this
section of code, you shouldn't be taking the word of anyone replying to
your question in this newsgroup. You should be measuring the actual
time and finding out the answer first-hand for yourself.

Pete

Nov 9 '07 #2
>Which one is faster or any other better way to do it.

You could consider calling Buffer.BlockCopy instead.

Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 9 '07 #3
On 2007-11-09 14:02:09 -0800, Mattias Sjögren
<ma********************@mvps.orgsaid:
>Which one is faster or any other better way to do it.

You could consider calling Buffer.BlockCopy instead.
Why? When an existing data copying method already exists, what
advantage would there be in using Buffer.BlockCopy()? Is it really so
much faster than just calling Array.Copy() to justify sacrificing
readability and type-consistency?

Pete

Nov 9 '07 #4
Peter,

In my opinion is this often investigated, why don't you do that yourself or
search these newsgroups (it can be that it was in the languages.vb
newsgroup).

(It is partially just joking reflecting your answer to Linuxfedora, what
Mattias wrote is well knowed, I was searched this answer in my mind and
Mattias answer was reminding it to me.)

Cor

Nov 10 '07 #5
On 2007-11-10 01:24:26 -0800, "Cor Ligthert[MVP]"
<no************@planet.nlsaid:
In my opinion is this often investigated, why don't you do that
yourself or search these newsgroups (it can be that it was in the
languages.vb newsgroup).
I have. But what happens when my own investigations disagree with
someone's claims? Should I not give them an opportunity to explain
themselves?
(It is partially just joking reflecting your answer to Linuxfedora,
what Mattias wrote is well knowed, I was searched this answer in my
mind and Mattias answer was reminding it to me.)
It appears to me that what "is well knowed [sic]" is frequently wrong.

And it appears to be so in this case as well. I did in fact write some
test code, and there is practically no difference at all between using
Array.Copy() and Buffer.BlockCopy(). In fact, in my tests Array.Copy()
came out slightly faster when copying within an array (and slightly
slower copying from one array to another).

The performance differences were in roughly the same ballpark as those
that exist simply because of the multi-tasking environment (i.e. the
resulting times for each test varied nearly as much from one execution
of a test to the next, as they do between the specific alternative
algorithms being tested). The largest difference I was able to
reproduce was a mere 5%, hardly consequential even if _all_ that the
code does is copy arrays, and a completely trivial difference for any
code that does anything else that's at all interesting.

It's frustrating to me to see so many people just blindy repeat
"conventional wisdom", things that they have "researched" (as you have
here) only by searching other posts in the newsgroups. As some wise
people in my life have always told me, "believe nothing you read, and
only half of what you see". A bit hyperbolic, but there's a very large
grain of truth in that statement.

So, the question still stands: does anyone have any _concrete_,
firsthand information that would suggest that using Buffer.BlockCopy()
in this situation is in fact a superior choice, considering the
tradeoffs involved?

Pete

Nov 10 '07 #6
>Why? When an existing data copying method already exists, what
>advantage would there be in using Buffer.BlockCopy()? Is it really so
much faster than just calling Array.Copy() to justify sacrificing
readability and type-consistency?
I'm just saying he should know what options are available. I'll leave
the benchmarking and decision of which method to use to the original
poster.

I don't see why calling Buffer.BlockCopy rather than Array.Copy would
sacrifice readability.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 11 '07 #7
On 2007-11-11 13:18:00 -0800, Mattias Sjögren
<ma********************@mvps.orgsaid:
I'm just saying he should know what options are available. I'll leave
the benchmarking and decision of which method to use to the original
poster.
You didn't say it was simply an alternative. You said it should be
used instead. That's not leaving "the decision of which method to use
to the original poster". You made a specific recommendation. I asked
why you did.
I don't see why calling Buffer.BlockCopy rather than Array.Copy would
sacrifice readability.
Well, for one, Buffer.BlockCopy() is byte-based, not element based,
which means that using it on arrays that aren't arrays of bytes has to
include code to convert numbers of elements to numbers of bytes.

IMHO, there are other issues as well. The fact that you're dealing
with an array means that methods on the Array class are more suitable
generally, at least from a readability point of view (especially if you
can use an instance method, which is often the case). It's a simple
matter of whether you have to go outside the class you're using or not.
IMHO, it's always more readable to stick with the class you're using,
than to use some helper class.

That's not to say helper classes don't have their place. But when
using one, it should be for a good reason.

Pete

Nov 11 '07 #8
>You didn't say it was simply an alternative. You said it should be
used instead. That's not leaving "the decision of which method to use
to the original poster". You made a specific recommendation. I asked
why you did.
Ok, maybe I could have phrased that better. You'll have to excuse my
English, it's far from perfect since it's not my native language. But
to me there's a significant difference between "could consider" and
"should be used".

>Well, for one, Buffer.BlockCopy() is byte-based, not element based,
which means that using it on arrays that aren't arrays of bytes has to
include code to convert numbers of elements to numbers of bytes.
The OP was working with byte[] so in this case that's a non-issue.

>IMHO, there are other issues as well. The fact that you're dealing
with an array means that methods on the Array class are more suitable
generally
Right, but I'm not talking about the general case. The OP specifically
asked for alternatives and that's what I replied to (ok, "any other
better way to do it", and I didn't mean to imply that Buffer.BlockCopy
is necessarily better) .

Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 12 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Shea Martin | last post by:
I have a MyString class: class MyString { public: MyString() {} MyString(const char *str) { _buffer = new char; strcpy(_buffer, str); }
6
by: PengYu.UT | last post by:
Hi, Suppose I have the following class declaration with a pointer as the argument. And suppose I "new" an array "lengths" as the argument "edge_lengths". Now, the polygon object is valid....
26
by: Brett | last post by:
I have created a structure with five fields. I then create an array of this type of structure and place the structure into an array element. Say index one. I want to assign a value to field3 of...
5
by: johannblake | last post by:
I need to copy all the data from a one dimensional byte array to a specific column in a two dimensional int array. For example: byte byteArray = {4, 5, 6}; int intArray = new int; After...
9
by: Nikolay Petrov | last post by:
I have a couple of byte arrays. What is the most efficient way to combine them? After combining they will go in a network stream. thanks
19
by: Tom Jastrzebski | last post by:
Hello, I was just testing VB.Net on Framework.Net 2.0 performance when I run into the this problem. This trivial code attached below executed hundreds, if not thousand times faster in VB 6.0...
21
by: yeti349 | last post by:
Hi, I'm using the following code to retrieve data from an xml file and populate a javascript array. The data is then displayed in html table form. I would like to then be able to sort by each...
8
by: tom | last post by:
Hi All, I'm stuck whit an issue I can't seem to resolve in C#: I have an arry of bytes which I would like to "recast" to an array of structs with an Explicit layout. I tried the...
4
by: Michel Esber | last post by:
Hello, LUW V8 FP 11 running Linux RH AS4 Update 3. In regards to performance and IO parallelism, does it matter if I create a tablespace with a single big container, or is it better to create...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.