【发布时间】:2019-07-31 08:02:09
【问题描述】:
在 VB .Net 中,正在构建一个类对象来读取二进制数据库文件并处理记录。
首先我获取二进制文件的全部内容,然后使用 .Skip().Take() 方法对其进行处理。不幸的是,这些方法会降低应用程序的速度。
我的问题是:有没有比使用 .Skip().Take() 方法更快的方法来处理数据?
下面是一些示例代码:
Public Sub ExampleCode(ByVal filename As String)
Dim length As Long
Dim content As Byte()
Dim records As New List(Of Array)
'Following constants are in actuality extracted from a header...
Const _NumberOfRecords = 2000000
Const _NumberOf Fields = 48
'Get the contant for specified file
content = My.Computer.FileSystem.ReadAllBytes(filename)
'Process all fields for this record
For j As Long = 0 To _NumberOfRecords - 1
Dim record(_NumberOfFields - 1)
lastPos = 1320
For i As Long = 0 To _NumberOfFields - 1
'Get the field length from_FieldDescriptors that is
'normally extracted from a fields section in the binary file
length = _FieldDescriptors(i).Length
'Get the value and add it to this record
record(i) = _Content.Skip(lastPos).Take(length).ToArray
'Proceed to the next field
lastPos += length
Next i
_Records.Add(record)
Next j
End Sub
【问题讨论】:
-
这行
_Records.Add(record)不应该是records.Add(record)吗? -
@preciousbetine 你是对的。在这种情况下,代码是从实际应用程序中复制的,其中 _Records 是 Records 的只读属性值...