MemoryBlock vrs Byte()

Is there any difference in performance between:
Unpacking an Int32 into myByte(3)
Dim mb as New MemoryBlock(4)
mb.xb.Int32Value(0) = x

The first takes me 30 lines of code the mb is 2 both are working fine, so just looking at performance.


I think it would also depend on how you want to get it OUT of the array or MB…
MB has functions to create integer, strings, etc.
with a an array of bytes, you have to do the reassembly yourself…

So… an MB would probably be the best choice (again not knowing you full intent)

There’s no reassembly of the bytes They are being used as a lookup in a myArray(byte(0)) or myArray(mb.byte(0))

The only thing to be careful about is the endianness of the memoryblock. Ie., the byte order when assigning an Int32 at a time vs. the order when assigning one byte at a time. But if it works (gives the expected results), the memoryblock is the way to go.

Hi thanks Tim, I think I did a quick and dirty test on 1M numbers and mb is twice as fast as splitting
Thanks, I’m testing speed (I think) and it seems to be twice as fast with MemoryBlock

Button1 Dim tick1 As Integer = Ticks dim A as Integer = 0 to 1000000 Dim mb As New MemoryBlock(4) mb.Int16Value(0) = a Next Dim tick2 as Integer = ticks

Dim tick1 As Integer = Ticks
For a As Integer = 0 to 1000000
Dim b(3) As Byte
b(0) = a And m_lOnBits(7)
b(1) = RShift(a, 8) And m_lOnBits(7)
b(2) = RShift(a, 16) And m_lOnBits(7)
b(3) = RShift(a, 24) And m_lOnBits(7)


Dim tick2 As Integer = Ticks[/code]

//comes to 32 ticks for mb verses 68 using bitwise
//comes to 4 verses 32 if I make mb a property of the class / b(3) a property of the class.

With the advantage of knowing how this is all implemented, I would say it is safe to assume that MemoryBlock will be faster for the foreseeable future (which your test shows).