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 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/>
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
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/>
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
"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/>
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
"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/>
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
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/> This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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
|
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
|
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
|
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();
| |
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...
|
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
|
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?
|
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
|
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...
|
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,...
| |
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...
|
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...
|
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...
|
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();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |