473,796 Members | 2,712 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Byte arrays perfromance question

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

Nov 21 '05 #1
9 1156
"Nikolay Petrov" <jo******@mail. bg> schrieb:
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.


\\\
Dim a1() As Byte = {1, 2, 3, 4, 5}
Dim a2() As Byte = {6, 7, 8, 9, 10}
Dim a(a1.Length + a2.Length - 1) As Byte
Buffer.BlockCop y(a1, 0, a, 0, a1.Length)
Buffer.BlockCop y(a2, 0, a, a1.Length, a2.Length)
///

Alternatively you can write the two arrays to the stream consecutively.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Nov 21 '05 #2
Nikolay,

Alternative from Herfrieds method using same sample.
(I find this easier to write)

\\\
Dim a1() As Byte = {1, 2, 3, 4, 5}
Dim a2() As Byte = {6, 7, 8, 9, 10}
Dim a(a1.Length + a2.Length - 1) As Byte
a1.CopyTo(a, 0)
a2.CopyTo(a, a1.Length)
////

I hope this helps,

Cor
Nov 21 '05 #3
Cor,

"Cor Ligthert" <no************ @planet.nl> schrieb:
Alternative from Herfrieds method using same sample.
(I find this easier to write)

\\\
Dim a1() As Byte = {1, 2, 3, 4, 5}
Dim a2() As Byte = {6, 7, 8, 9, 10}
Dim a(a1.Length + a2.Length - 1) As Byte
a1.CopyTo(a, 0)
a2.CopyTo(a, a1.Length)
////


Note that the performance of 'Buffer.BlockCo py' is better than those of
'Array.CopyTo', and the OP asked for a fast solution.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #4
Herfried,

Note that the performance of 'Buffer.BlockCo py' is better than those of
'Array.CopyTo', and the OP asked for a fast solution.


I thought let me make a sample to show how right Herfried is in his answer,
your sample is almost 0,0015 times faster. You can try it, the sample that
does 100.000.000 loops needs depending on the computer 1 to 5 minutes I
assume. On your fast computer probably less than 2 minutes.

\\\
Public Module My
Sub Main()
Dim a1() As Byte = {1, 2, 3, 4, 5}
Dim a2() As Byte = {6, 7, 8, 9, 10}
Dim a(a1.Length + a2.Length - 1) As Byte
For y As Integer = 0 To 9
Dim steps As Integer = 10000000
Dim test1 As Integer = Environment.Tic kCount
For i As Integer = 0 To steps
Buffer.BlockCop y(a1, 0, a, 0, a1.Length)
Buffer.BlockCop y(a2, 0, a, a1.Length, a2.Length)
Next
test1 = Environment.Tic kCount - 1
Dim test2 As Integer = Environment.Tic kCount
For i As Integer = 0 To steps
a1.CopyTo(a, 0)
a2.CopyTo(a, a1.Length)
Next
test2 = Environment.Tic kCount - 1
Console.Write(" Methode as provided by Herfried = " _
& (1 - test1 / test2).ToString & " faster than from Cor" &
vbCrLf)
Next
End Sub
End Module
Methode as provided by Herfried = 0,0014613291761 3373 faster than from Cor
Methode as provided by Herfried = 0,0014760397359 268 faster than from Cor
Methode as provided by Herfried = 0,0014788083862 9866 faster than from Cor
Methode as provided by Herfried = 0,0014645579206 1673 faster than from Cor
Methode as provided by Herfried = 0,0014618244886 111 faster than from Cor
Methode as provided by Herfried = 0,0014473067397 0261 faster than from Cor
Methode as provided by Herfried = 0,0014622915571 3606 faster than from Cor
Methode as provided by Herfried = 0,0014478182331 6897 faster than from Cor
Methode as provided by Herfried = 0,0014510005590 3713 faster than from Cor
///

You don't mind that I keep it in this newsgroup for the method, which is for
me easier to remember and less characters to write.

:-)

Cor
Nov 21 '05 #5
"Cor Ligthert" <no************ @planet.nl> schrieb:
Note that the performance of 'Buffer.BlockCo py' is better than those of
'Array.CopyTo', and the OP asked for a fast solution.


I thought let me make a sample to show how right Herfried is in his
answer, your sample is almost 0,0015 times faster. You can try it, the
sample that does 100.000.000 loops needs depending on the computer 1 to 5
minutes I assume. On your fast computer probably less than 2 minutes.


I didn't test it and I won't test it. The documentation of 'Buffer' states
that the performance is better than with 'System.Array', and that's why the
'Buffer' class exists:

| 'Buffer' provides methods to copy bytes from one array of
| primitive types to another array of primitive types, get a
| byte from an array, set a byte in an array, and obtain the length
| of an array. This class provides better performance for manipulating
| primitive types than similar methods in the 'System.Array' class.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #6
If you correct your test code you'll find that Herfried's BlockCopy is about
70% to 75% faster.

Line 13: test1 = Environment.Tic kCount - 1
-> test1 = Environment.Tic kCount - test1

Line 19: test2 = Environment.Tic kCount - 1
-> test2 = Environment.Tic kCount - test2
"Cor Ligthert" <no************ @planet.nl> wrote in message
news:%2******** **********@TK2M SFTNGP15.phx.gb l...
Herfried,

Note that the performance of 'Buffer.BlockCo py' is better than those of
'Array.CopyTo', and the OP asked for a fast solution.


I thought let me make a sample to show how right Herfried is in his
answer, your sample is almost 0,0015 times faster. You can try it, the
sample that does 100.000.000 loops needs depending on the computer 1 to 5
minutes I assume. On your fast computer probably less than 2 minutes.

\\\
Public Module My
Sub Main()
Dim a1() As Byte = {1, 2, 3, 4, 5}
Dim a2() As Byte = {6, 7, 8, 9, 10}
Dim a(a1.Length + a2.Length - 1) As Byte
For y As Integer = 0 To 9
Dim steps As Integer = 10000000
Dim test1 As Integer = Environment.Tic kCount
For i As Integer = 0 To steps
Buffer.BlockCop y(a1, 0, a, 0, a1.Length)
Buffer.BlockCop y(a2, 0, a, a1.Length, a2.Length)
Next
test1 = Environment.Tic kCount - 1
Dim test2 As Integer = Environment.Tic kCount
For i As Integer = 0 To steps
a1.CopyTo(a, 0)
a2.CopyTo(a, a1.Length)
Next
test2 = Environment.Tic kCount - 1
Console.Write(" Methode as provided by Herfried = " _
& (1 - test1 / test2).ToString & " faster than from Cor" &
vbCrLf)
Next
End Sub
End Module
Methode as provided by Herfried = 0,0014613291761 3373 faster than from Cor
Methode as provided by Herfried = 0,0014760397359 268 faster than from Cor
Methode as provided by Herfried = 0,0014788083862 9866 faster than from Cor
Methode as provided by Herfried = 0,0014645579206 1673 faster than from Cor
Methode as provided by Herfried = 0,0014618244886 111 faster than from Cor
Methode as provided by Herfried = 0,0014473067397 0261 faster than from Cor
Methode as provided by Herfried = 0,0014622915571 3606 faster than from Cor
Methode as provided by Herfried = 0,0014478182331 6897 faster than from Cor
Methode as provided by Herfried = 0,0014510005590 3713 faster than from Cor
///

You don't mind that I keep it in this newsgroup for the method, which is
for me easier to remember and less characters to write.

:-)

Cor

Nov 21 '05 #7
"Stephen Martin" <sm*****@remove this.emsoft.and this.ca> schrieb:
If you correct your test code you'll find that Herfried's BlockCopy is
about 70% to 75% faster.

Line 13: test1 = Environment.Tic kCount - 1
-> test1 = Environment.Tic kCount - test1

Line 19: test2 = Environment.Tic kCount - 1
-> test2 = Environment.Tic kCount - test2


Good catch!

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #8
Stephen,

Thanks I did not see it, and I knew that can not be so thight together.I
think that I have ten times overlooked this part, because there I know I
make often an error.

Just blind for the code

Cor

Methode as provided by Herfried = 0,7252076209086 47 faster than from Cor
Methode as provided by Herfried = 0,71475 faster than from Cor
Methode as provided by Herfried = 0,7172619047619 05 faster than from Cor
Methode as provided by Herfried = 0,71875 faster than from Cor
Methode as provided by Herfried = 0,7142494284988 57 faster than from Cor
Methode as provided by Herfried = 0,7221234442468 88 faster than from Cor
Methode as provided by Herfried = 0,7221940071102 08 faster than from Cor
Methode as provided by Herfried = 0,7224479431183 34 faster than from Cor
Methode as provided by Herfried = 0,7209895434838 05 faster than from Cor
Nov 21 '05 #9
Buffer.BlockCop y = 2.9 secs
CopyTo = 8.2 secs
Array.Copy = 7.5 secs

Enough said.

"Herfried K. Wagner [MVP]" <hi************ ***@gmx.at> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
"Stephen Martin" <sm*****@remove this.emsoft.and this.ca> schrieb:
If you correct your test code you'll find that Herfried's BlockCopy is
about 70% to 75% faster.

Line 13: test1 = Environment.Tic kCount - 1
-> test1 = Environment.Tic kCount - test1

Line 19: test2 = Environment.Tic kCount - 1
-> test2 = Environment.Tic kCount - test2


Good catch!

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #10

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

Similar topics

6
6319
by: Harry Overs | last post by:
My program needs to take a pointer to BYTE array (unsigned char*) and convert it into a STL list so that each BYTE in the array has its own element in the list, i.e. if the array has hundred bytes then the list needs to have a hundred entries, at present when I try to do this each element in my list points to the entire BYTE array, when what I really need is copies of each single BYTE in its own part of the list. I have used code similar...
8
13538
by: Tom Bean | last post by:
I have several data files containing ASCII strings, int, long and byte data types and need to store the data in variables of the appropriate type. From previous posts, there seem to be several methods for converting byte to strings. What is the best way to convert a byte to a long or int? Thanks, Tom
1
7231
by: Eric Hendriks | last post by:
// In an unmanaged DLL the following function must be called: // int VFGeneralize(const BYTE * const * features); // "features" parameter is supposed to be an array of byte arrays. // function is Marshaled as follows: static extern int VFGeneralize(byte features); In C# I have the following: // Allocate memory to store "Count" references to byte arrays
8
3410
by: Ben Terry | last post by:
What's the most efficient way to transfer data from a byte to a struct? The struct is rather complex--contains other structs as well as byte members. I've tried to use Marshal.Copy and an IntPtr to my struct address but I get the following error, perhaps due to the byte members of my struct: Cannot take the address or size of a variable of a managed type. Ben
7
6889
by: Joseph Lee | last post by:
Hi All, I am having problem when i am using hashtable to keep an array of bytes value as keys. Take a look at the code snippet below --------------------------------------------------- ASCIIEncoding asciiEncoder = new ASCIIEncoding();
6
2786
by: Dennis | last post by:
I was trying to determine the fastest way to build a byte array from components where the size of the individual components varied depending on the user's input. I tried three classes I built: (1) using redim arrays to add to a normal byte array (2) using an ArrayList and finally (3) using a memorystream. These three classes are listed below the test sub called "TestBuildByteArray". It was interesting that using the memorystream was...
6
4682
by: Vitaly Zayko | last post by:
It's probably simple but I can't find a way how to copy number of bytes from one byte array to another? Just like Array.Copy(SourceArray, SourceIndex, DestArray, DestIndex, Length) does but in my case the arrays have different length. Of course I can copy byte by byte but I guess there should be a function for such task. Thanks! -- Vit Zayko
17
7256
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need to show the array data to the end user. Can I do that? How?
77
4305
by: borophyll | last post by:
As I read it, C99 states that a byte is an: "addressable unit of data storage large enough to hold any member of the basic character set of the execution environment" (3.6) and that a byte must be at least 8 bits: "The values given below shall be replaced by constant expressions suitable for use in #if
0
9673
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9524
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
10449
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10217
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
10003
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...
0
6785
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
5440
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...
0
5568
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.