2014-05-10 150 views
1

嗯,我試圖將各種數據「字節」轉換爲「長」。 它似乎很慢...緩慢處理

代碼:

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 

轉換器:

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 

任何人有提示或解決方案?

+2

是什麼讓你覺得這很慢? – Enigmativity

回答

0

我可以看到的一個問題是,您每次讀取長整型值時都會調用buff.ToArrayToArray方法會每次創建一個緩衝區的副本。在開始處理地圖之前,您應該調用ToArray,並在調用BitConverter.ToInt64方法時使用數組實例。