代码之家  ›  专栏  ›  技术社区  ›  Gforse

使用Filestream分析十六进制文件并行

  •  0
  • Gforse  · 技术社区  · 7 年前

    所以我想加快 For i as integer

    以下是电流示例:

    Using fs As New FileStream(PathToFile, FileMode.Open, FileAccess.Read, FileShare.Read)    
         For i As Long = 0 To fs.Length Step 1024
    
              'Go to the calculated index in the file
              fs.Seek(i, SeekOrigin.Begin)
    
              'Now get 24 bytes at the current index
              fs.Read(buffer, 0, 24)
    
              'Do some stuff with it
              List.Add(Buffer)
          Next
    End Using
    

    Parrallel.For

    1 回复  |  直到 7 年前
        1
  •  0
  •   David Wilson    7 年前

    这可能会被标记下来,但如果内存使用不是一个大问题,那么我建议读取整个文件并将这些24字节序列存储在字节数组中(23)。然后使用 Parallel.For

    试试这个。。

    Imports System.Math
    Imports System.IO
    
    Public Class Form1
        'create the data array, with just 1 element to start with
        'so that it can be resized then you know how many 1024 byte
        'chunks you have in your file
        Dim DataArray(1)() As Byte
    
    
        Private Sub ReadByteSequences(pathtoFile As String)
            Using fs As New FileStream(pathtoFile, FileMode.Open, FileAccess.Read, FileShare.Read)
                'resize the array when you have the file size
                ReDim DataArray(CInt(Math.Floor(fs.Length) / 1024))
                For i As Long = 0 To fs.Length Step 1024
                    Dim buffer(23) As Byte
                    fs.Seek(i, SeekOrigin.Begin)
                    fs.Read(buffer, 0, 24)
                    'store each 24 byte sequence in order in the 
                    'DataArray for later processing
                    DataArray(CInt(Math.Floor(i / 1024))) = buffer
                Next
            End Using
        End Sub
    
        Private Sub ProcessDataArray()
            Parallel.For(0, DataArray.Length, Sub(i As Integer)
                                                  'do atuff in parallel
                                              End Sub)
        End Sub
    
    End Class