455,887 Members | 1,222 Online
Need help? Post your question and get tips & solutions from a community of 455,887 IT Pros & Developers. It's quick & easy.

# Byte arrays perfromance question

 P: n/a 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 Replies

 P: n/a "Nikolay Petrov" 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.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. -- M S Herfried K. Wagner M V P V B Nov 21 '05 #2

 P: n/a 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

 P: n/a Cor, "Cor Ligthert" 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.BlockCopy' is better than those of 'Array.CopyTo', and the OP asked for a fast solution. -- M S Herfried K. Wagner M V P V B Nov 21 '05 #4

 P: n/a 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 Nov 21 '05 #5

 P: n/a "Cor Ligthert" schrieb: 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. 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 V B Nov 21 '05 #6

 P: n/a 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 "Cor Ligthert" wrote in message news:%2******************@TK2MSFTNGP15.phx.gbl... 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 Nov 21 '05 #7

 P: n/a "Stephen Martin" schrieb: 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! -- M S Herfried K. Wagner M V P V B Nov 21 '05 #8

 P: n/a 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 Nov 21 '05 #9

 P: n/a Buffer.BlockCopy = 2.9 secs CopyTo = 8.2 secs Array.Copy = 7.5 secs Enough said. "Herfried K. Wagner [MVP]" wrote in message news:%2****************@TK2MSFTNGP09.phx.gbl... "Stephen Martin" schrieb: 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! -- M S Herfried K. Wagner M V P V B Nov 21 '05 #10

### This discussion thread is closed

Replies have been disabled for this discussion.