Byte arrays perfromance question

  • Thread starter Thread starter Nikolay Petrov
  • Start date Start date
N

Nikolay Petrov

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
 
Nikolay Petrov said:
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.BlockCopy(a1, 0, a, 0, a1.Length)
Buffer.BlockCopy(a2, 0, a, a1.Length, a2.Length)
///

Alternatively you can write the two arrays to the stream consecutively.
 
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 said:
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.BlockCopy' is better than those of
'Array.CopyTo', and the OP asked for a fast solution.
 
Herfried,
Note that the performance of 'Buffer.BlockCopy' 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.TickCount
For i As Integer = 0 To steps
Buffer.BlockCopy(a1, 0, a, 0, a1.Length)
Buffer.BlockCopy(a2, 0, a, a1.Length, a2.Length)
Next
test1 = Environment.TickCount - 1
Dim test2 As Integer = Environment.TickCount
For i As Integer = 0 To steps
a1.CopyTo(a, 0)
a2.CopyTo(a, a1.Length)
Next
test2 = Environment.TickCount - 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,00146132917613373 faster than from Cor
Methode as provided by Herfried = 0,0014760397359268 faster than from Cor
Methode as provided by Herfried = 0,00147880838629866 faster than from Cor
Methode as provided by Herfried = 0,00146455792061673 faster than from Cor
Methode as provided by Herfried = 0,0014618244886111 faster than from Cor
Methode as provided by Herfried = 0,00144730673970261 faster than from Cor
Methode as provided by Herfried = 0,00146229155713606 faster than from Cor
Methode as provided by Herfried = 0,00144781823316897 faster than from Cor
Methode as provided by Herfried = 0,00145100055903713 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 said:
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.
 
If you correct your test code you'll find that Herfried's BlockCopy is about
70% to 75% faster.

Line 13: test1 = Environment.TickCount - 1
-> test1 = Environment.TickCount - test1

Line 19: test2 = Environment.TickCount - 1
-> test2 = Environment.TickCount - test2
 
Stephen Martin said:
If you correct your test code you'll find that Herfried's BlockCopy is
about 70% to 75% faster.

Line 13: test1 = Environment.TickCount - 1
-> test1 = Environment.TickCount - test1

Line 19: test2 = Environment.TickCount - 1
-> test2 = Environment.TickCount - test2

Good catch!
 
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,725207620908647 faster than from Cor
Methode as provided by Herfried = 0,71475 faster than from Cor
Methode as provided by Herfried = 0,717261904761905 faster than from Cor
Methode as provided by Herfried = 0,71875 faster than from Cor
Methode as provided by Herfried = 0,714249428498857 faster than from Cor
Methode as provided by Herfried = 0,722123444246888 faster than from Cor
Methode as provided by Herfried = 0,722194007110208 faster than from Cor
Methode as provided by Herfried = 0,722447943118334 faster than from Cor
Methode as provided by Herfried = 0,720989543483805 faster than from Cor
 
Buffer.BlockCopy = 2.9 secs
CopyTo = 8.2 secs
Array.Copy = 7.5 secs

Enough said.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top