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



我想做的:

  1. 打开两个二进制文件,每个64mb
  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分钟内写入1mb。我如何优化它?

文件足够小,可以加载到RAM中进行处理。在RAM中处理数据可以最大限度地减少所需的磁盘I/O,后者通常是程序中最慢的部分,特别是当它以非常小的块(如单个字节)完成时。正如Ben Voigt所指出的,字符串操作比数值操作要慢一些。

这里有一个简单的演示,可以做什么:

Imports System.IO
Module Module1
Dim f1 As String = "C:tempA.bin"
Dim f2 As String = "C:tempB.bin"
Dim outFile As String = "C:tempC.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

当然,您需要检查输入文件的长度是否相等,并检查是否存在任何其他可能的问题;)

相关内容

  • 没有找到相关文章

最新更新