【问题标题】:Skipping through a byte array跳过字节数组
【发布时间】: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 的只读属性值...

标签: arrays vb.net


【解决方案1】:

如果_ContentArray,你可以试试Array.Copy,应该会更快:

record(i) = New Byte(length) {}

Array.Copy(_Content, lastPos, record(i), 0, length);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-13
    • 1970-01-01
    • 1970-01-01
    • 2012-11-20
    • 1970-01-01
    • 2015-08-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多