Well, I'm trying to convert various data "bytes" to "long". And it seems to be very slow ...
Code:
For X = 0 To Map.MaxX
For Y = 0 To Map.MaxY
Map.Tile(X, Y).Data1 = Buffer.ReadLong
Map.Tile(X, Y).Data2 = Buffer.ReadLong
Map.Tile(X, Y).Data3 = Buffer.ReadLong
Map.Tile(X, Y).DirBlock = Buffer.ReadLong
ReDim Map.Tile(X, Y).Layer(0 To MapLayer.Layer_Count - 1)
For i = 0 To MapLayer.Layer_Count - 1
Map.Tile(X, Y).Layer(i).tileset = Buffer.ReadLong
Map.Tile(X, Y).Layer(i).X = Buffer.ReadLong
Map.Tile(X, Y).Layer(i).Y = Buffer.ReadLong
Next
Map.Tile(X, Y).Type = Buffer.ReadLong
Next
Next
Converter:
Public Function ReadLong(Optional ByVal peek As Boolean = True) As Long
If Buff.Count > readpos Then 'check to see if this passes the byte count
Dim ret As Long = BitConverter.ToInt64(Buff.ToArray, readpos)
If peek And Buff.Count > readpos Then
readpos += 8
End If
Return ret
Else
Throw New Exception("Byte Buffer Past Limit!") 'past byte count throw a new exception
End If
End Function
Anyone have tips or a solution?
One problem I can see is that you are calling buff.ToArray
each time you read a long value. The ToArray
method will make a copy of the buffer each time. You should call ToArray
before you start processing the map and use the array instance when calling the BitConverter.ToInt64
method.