473,785 Members | 2,777 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(send Buffer,
realDataSent, sendBuffer, 0, sendBuffer.Leng th - realDataSent);
and this one:
byte[] tempBuffer =
(byte[])sendBuffer.Clo ne();
Array.Clear(sen dBuffer, 0,
sendBuffer.Leng th);
Array.Copy(temp Buffer,
realDataSent, sendBuffer, 0, tempBuffer.Leng th - realDataSent);

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

Thanks

Nov 9 '07 #1
8 7444
On 2007-11-08 19:36:23 -0800, li*********@yah oo.com.hk said:
[...]
realDataSent, sendBuffer, 0, sendBuffer.Leng th - realDataSent);
and this one:
byte[] tempBuffer =
(byte[])sendBuffer.Clo ne();
Array.Clear(sen dBuffer, 0,
sendBuffer.Leng th);
Array.Copy(temp Buffer,
realDataSent, sendBuffer, 0, tempBuffer.Leng th - 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.BlockCop y 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.o rgsaid:
>Which one is faster or any other better way to do it.

You could consider calling Buffer.BlockCop y instead.
Why? When an existing data copying method already exists, what
advantage would there be in using Buffer.BlockCop y()? 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.BlockCop y(). 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
"convention al 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.BlockCop y()
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.BlockCop y()? 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.BlockCop y 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.o rgsaid:
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.BlockCop y rather than Array.Copy would
sacrifice readability.
Well, for one, Buffer.BlockCop y() 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.BlockCop y() 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.BlockCop y
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
371
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
1449
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. Unfortunately, it becomes invalid after I delete "lengths" array be mistake. My question is that using object, the object's(an polygon object in this case) internal states are encapsulated. They should not be affect by anything operations unless the...
26
7096
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 the structure inside the array. When I try this, an error about late assignment appears. Is it possible to assign a value to a structure field that is in an array? I'm currently getting around the problem by creating a new structure, assign...
5
2662
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 copying to the first column in intArray, I want the intArray to have the following data: intArray has the value 4
9
1156
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
3155
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 than in .Net environment, under VS 2005 Beta 2. Does anyone have any idea whether this will be addressed in the final release? Thanks, Tomasz
21
3224
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 column. Once the array elements are split, what is the best way to sort them? Thank you. //populate data object with data from xml file. //Data is a comma delimited list of values var jsData = new Array(); jsData = {lib: "#field...
8
5094
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 Buffer.BlockCopy method, but that one complains my struct is not a primitive type. Any Suggestions ?
4
1795
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 it with several smaller containers ? Any ideas and links to the proper documentation is greatly appreciated.
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10151
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7499
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6740
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3647
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.