与 Filestream 并行解析 HEX 文件
Parse HEX file Parrallel with Filestream
所以我正在尝试加快 For i as integer
以获得最大性能。我在我的代码中使用了越来越多的 Parrallel 和 Async 方法,这对我有很大帮助。但是目前我坚持使用这个。我只是简单地循环遍历一个文件,读取特定的索引位置,以查看文件中的内容,以便稍后我可以用它做某些事情。
这是当前For的例子:
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
文件的大小可以是 15MB 到 4GB。目前我被困在如何在 Parrallel.For
中使用步骤 1024 以及如何使其线程安全。希望有人能帮我解决这个问题。
这可能会被标记下来,但如果内存使用不是一个大问题,那么我会建议读取整个文件并将那些 24 字节序列存储在字节 (23) 的数组中。然后使用 Parallel.For
处理数组。在我的电脑上,阵列占用大约 160mb 的 4gb。读取速度当然取决于所使用的系统。在我的电脑上大约需要 25 秒。
试试这个..
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
所以我正在尝试加快 For i as integer
以获得最大性能。我在我的代码中使用了越来越多的 Parrallel 和 Async 方法,这对我有很大帮助。但是目前我坚持使用这个。我只是简单地循环遍历一个文件,读取特定的索引位置,以查看文件中的内容,以便稍后我可以用它做某些事情。
这是当前For的例子:
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
文件的大小可以是 15MB 到 4GB。目前我被困在如何在 Parrallel.For
中使用步骤 1024 以及如何使其线程安全。希望有人能帮我解决这个问题。
这可能会被标记下来,但如果内存使用不是一个大问题,那么我会建议读取整个文件并将那些 24 字节序列存储在字节 (23) 的数组中。然后使用 Parallel.For
处理数组。在我的电脑上,阵列占用大约 160mb 的 4gb。读取速度当然取决于所使用的系统。在我的电脑上大约需要 25 秒。
试试这个..
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