如何更快地使用二进制文件操作?

发布于 2025-02-12 15:31:53 字数 3142 浏览 0 评论 0原文

我要做的是:

  1. 打开两个二进制文件,每个
  2. 文件的每个字节的前半部分都在每个文件的一个字节中,并将这些半字节组合在1字节中,例如:第一个文件中的第一个字节是0x51,在第二个文件中归档其0xa2,因此我需要在第三个文件中写两个字节,即0x5a和0x12,对于整个字节相同,因此第三个文件中的最终长度为128 MB。
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles 
       Button1.Click
      Try                                                                             
    ' choosing first file
        OpenFileDialog1.FileName = "First file"
        OpenFileDialog1.Title = "Choose the Address.bin file"
        OpenFileDialog1.Filter = "bin files (*.bin)|*.bin|All files 
         (*.*)|*.*"
        If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK 
         Then
            Label1.Text = 
         System.IO.Path.GetFullPath(OpenFileDialog1.FileName)

        Else
            Exit Sub
        End If
    Catch ex As Exception
    End Try
    Try                                                                               ' choosing first file
        OpenFileDialog1.FileName = "Second FIle"
        OpenFileDialog1.Title = "Choose the Flash.bin file"
        OpenFileDialog1.Filter = "bin files (*.bin)|*.bin|All files (*.*)|*.*"
        If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Label2.Text = System.IO.Path.GetFullPath(OpenFileDialog1.FileName)

        Else
            Exit Sub
        End If
    Catch ex As Exception
    End Try
    Dim firstFileByte(1) As Byte
    Dim SecondFileByte(1) As Byte
    Dim Result As String
    Dim Result2 As String
    Dim Final(1) As Byte

    For i = 0 To FileLen(Label1.Text) - 1



        Using FirstFile As New FileStream(Label1.Text, FileMode.Open) 'save

            'FIRST DIGIT********************************************************************************************
            FirstFile.Seek(i, SeekOrigin.Begin)
            FirstFile.Read(firstFileByte, 0, 1)

            'TextBox1.Text = final(0).ToString("X")
            Using SecFile As New FileStream(Label2.Text, FileMode.Open) 'save
                SecFile.Seek(i, SeekOrigin.Begin)
                SecFile.Read(SecondFileByte, 0, 1)
            End Using
            Result = firstFileByte(0).ToString("X2").Substring(0, 1) & SecondFileByte(0).ToString("X2").Substring(0, 1)   ' comobining frist half of the first file and second file 
            Result2 = firstFileByte(0).ToString("X2").Substring(1, 1) & SecondFileByte(0).ToString("X2").Substring(1, 1) ' comobining second half of the first file and second file  

        End Using
        Using vFs As New FileStream(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\Result.bin", FileMode.Append) '  save
            TextBox1.Text = Result2
            'Dim FileLenVar As UInt32 = FileLen(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\Result.bin") - 1

            Final(0) = Convert.ToByte(Result, 16)    'converting result to the byte
            Final(1) = Convert.ToByte(Result2, 16)

            vFs.Write(Final, 0, 1)

            vFs.Write(Final, 1, 1)

        End Using
    Next
End Sub

它有效,但需要很多时间:它在1分钟内写入1 MB。如何优化它?

What I'm trying to do:

  1. Open two binary files, each 64 MB
  2. Take first half of each byte of each file and combine those halves in 1 bytes same with second half, for example: first byte in first file is 0x51, in second file its 0xA2, so I need write two bytes in third file which are 0x5A and 0x12, same for whole bytes, therefore final length in third file will be 128 MB.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles 
       Button1.Click
      Try                                                                             
    ' choosing first file
        OpenFileDialog1.FileName = "First file"
        OpenFileDialog1.Title = "Choose the Address.bin file"
        OpenFileDialog1.Filter = "bin files (*.bin)|*.bin|All files 
         (*.*)|*.*"
        If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK 
         Then
            Label1.Text = 
         System.IO.Path.GetFullPath(OpenFileDialog1.FileName)

        Else
            Exit Sub
        End If
    Catch ex As Exception
    End Try
    Try                                                                               ' choosing first file
        OpenFileDialog1.FileName = "Second FIle"
        OpenFileDialog1.Title = "Choose the Flash.bin file"
        OpenFileDialog1.Filter = "bin files (*.bin)|*.bin|All files (*.*)|*.*"
        If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Label2.Text = System.IO.Path.GetFullPath(OpenFileDialog1.FileName)

        Else
            Exit Sub
        End If
    Catch ex As Exception
    End Try
    Dim firstFileByte(1) As Byte
    Dim SecondFileByte(1) As Byte
    Dim Result As String
    Dim Result2 As String
    Dim Final(1) As Byte

    For i = 0 To FileLen(Label1.Text) - 1



        Using FirstFile As New FileStream(Label1.Text, FileMode.Open) 'save

            'FIRST DIGIT********************************************************************************************
            FirstFile.Seek(i, SeekOrigin.Begin)
            FirstFile.Read(firstFileByte, 0, 1)

            'TextBox1.Text = final(0).ToString("X")
            Using SecFile As New FileStream(Label2.Text, FileMode.Open) 'save
                SecFile.Seek(i, SeekOrigin.Begin)
                SecFile.Read(SecondFileByte, 0, 1)
            End Using
            Result = firstFileByte(0).ToString("X2").Substring(0, 1) & SecondFileByte(0).ToString("X2").Substring(0, 1)   ' comobining frist half of the first file and second file 
            Result2 = firstFileByte(0).ToString("X2").Substring(1, 1) & SecondFileByte(0).ToString("X2").Substring(1, 1) ' comobining second half of the first file and second file  

        End Using
        Using vFs As New FileStream(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\Result.bin", FileMode.Append) '  save
            TextBox1.Text = Result2
            'Dim FileLenVar As UInt32 = FileLen(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\Result.bin") - 1

            Final(0) = Convert.ToByte(Result, 16)    'converting result to the byte
            Final(1) = Convert.ToByte(Result2, 16)

            vFs.Write(Final, 0, 1)

            vFs.Write(Final, 1, 1)

        End Using
    Next
End Sub

It works, but takes a lot of time: it writes 1 MB in 1 minute. How can I optimize it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

祁梦 2025-02-19 15:31:53

这些文件足够小,可以加载到RAM进行处理。处理RAM中的数据可以最大程度地减少所需的磁盘I/O,而后者通常是程序中最慢的部分,尤其是如果它以非常小的单个字节(例如单个字节)进行操作。以及

这是一个简单的证明可以做的事情:

Imports System.IO

Module Module1
    Dim f1 As String = "C:\temp\A.bin"
    Dim f2 As String = "C:\temp\B.bin"
    Dim outFile As String = "C:\temp\C.bin"

    Sub CombineFiles()
        Dim a1 = File.ReadAllBytes(f1)
        Dim a2 = File.ReadAllBytes(f2)
        Dim c(a1.Length + a2.Length - 1) As Byte ' c for combined

        Dim highBits As Byte = &HF0
        Dim lowBits As Byte = &HF

        For i = 0 To c.Length - 1 Step 2
            c(i) = a1(i \ 2) And highBits Or a2(i \ 2) >> 4
            c(i + 1) = a1(i \ 2) << 4 Or a2(i \ 2) And lowBits
        Next

        File.WriteAllBytes(outFile, c)

    End Sub

    Sub CreateTestFiles()
        'TODO: be more creative with the generated data.

        Dim nBytes = 64
        Dim a(nBytes - 1) As Byte

        For i = 0 To nBytes - 1
            a(i) = &H12
        Next

        File.WriteAllBytes(f1, a)

        For i = 0 To nBytes - 1
            a(i) = &HAB
        Next

        File.WriteAllBytes(f2, a)

    End Sub

    Sub Main()
        'CreateTestFiles()
        CombineFiles()

    End Sub

End Module

当然,检查输入文件是否相等,并检查任何其他可能的问题;)

The files are small enough to load into RAM for processing. Processing the data in RAM can minimise the disk I/O needed, the latter very often being the slowest part of a program, especially if it is done in very small pieces like individual bytes. As also noted by Ben Voigt, string operations are somewhat slower than numeric operations.

Here's a simple demonstration of what could be done:

Imports System.IO

Module Module1
    Dim f1 As String = "C:\temp\A.bin"
    Dim f2 As String = "C:\temp\B.bin"
    Dim outFile As String = "C:\temp\C.bin"

    Sub CombineFiles()
        Dim a1 = File.ReadAllBytes(f1)
        Dim a2 = File.ReadAllBytes(f2)
        Dim c(a1.Length + a2.Length - 1) As Byte ' c for combined

        Dim highBits As Byte = &HF0
        Dim lowBits As Byte = &HF

        For i = 0 To c.Length - 1 Step 2
            c(i) = a1(i \ 2) And highBits Or a2(i \ 2) >> 4
            c(i + 1) = a1(i \ 2) << 4 Or a2(i \ 2) And lowBits
        Next

        File.WriteAllBytes(outFile, c)

    End Sub

    Sub CreateTestFiles()
        'TODO: be more creative with the generated data.

        Dim nBytes = 64
        Dim a(nBytes - 1) As Byte

        For i = 0 To nBytes - 1
            a(i) = &H12
        Next

        File.WriteAllBytes(f1, a)

        For i = 0 To nBytes - 1
            a(i) = &HAB
        Next

        File.WriteAllBytes(f2, a)

    End Sub

    Sub Main()
        'CreateTestFiles()
        CombineFiles()

    End Sub

End Module

Of course, you'd check that the input files were of equal length, and check for any other possible problems ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文