Как ускорить время выполнения программы в VB
Программа которая "шифрует" файл. Открывает файл в формате байтов и переписывает их значение на 10 больше, если значение больше 255, записывает остаток от деления на 256. Вторая часть "расшифровывает" файл по такому же принципу. С фото в 4к производит "шифровку" или "расшифровку" за 6-7 секунд. Но если делать тоже самое с видео весом 1.5 гигабайта, то процесс займёт 11,5 дней. Как ускорить время выполнения программы. Язык VB.
Module Module1
Sub Main()
Console.Write("Введите тип операции, шифровка (0), дешифровка (1) - ")
Dim z As Integer = Console.ReadLine()
Console.Write("Введите имя файла - ")
Dim x As String = Console.ReadLine()
If z = 0 Then
arx(x)
Else
dea(x)
End If
End Sub
Sub arx(a As String)
FileOpen(1, a, OpenMode.Binary)
Dim b(FileLen(a)) As Byte
Dim t As Integer
For i = 1 To UBound(b)
FileGet(1, b(i))
Next
FileClose(1)
For i = 1 To UBound(b)
t = b(i)
b(i) = (t + 10) Mod 256
Next
FileOpen(1, a, OpenMode.Output)
FileClose(1)
FileOpen(1, a, OpenMode.Binary)
For i = 1 To UBound(b)
FilePut(1, b(i))
Next
FileClose()
End Sub
Sub dea(a As String)
FileOpen(1, a, OpenMode.Binary)
Dim b(FileLen(a)) As Byte
Dim t As Integer
For i = 1 To UBound(b)
FileGet(1, b(i))
Next
FileClose(1)
For i = 1 To UBound(b)
t = b(i)
If b(i) < 10 Then
b(i) = t + 246
Else
b(i) -= 10
End If
Next
FileOpen(1, a, OpenMode.Output)
FileClose(1)
FileOpen(1, a, OpenMode.Binary)
For i = 1 To UBound(b)
FilePut(1, b(i))
Next
FileClose()
End Sub
End Module
Процедура arx() - Шифрует
Процедура dea() - Расшифровывает
Я подозреваю, скорость можно увеличить переписав программу на C или сделать многопоточность. Как сделать программу многопоточной не знаю.
Ответы (1 шт):
Попробуй прочитать файл одним потоком, это как минимум должно сократить время чтения файла и его кеширования как массив.
''' <summary>
''' Загрузить файл в Byte()
''' </summary>
''' <param name="filePath"></param>
''' <returns></returns>
Public Shared Function LoadFileAsByte(filePath As String) As Byte()
If System.IO.File.Exists(filePath) Then 'Если файл есть, то прочитать
Dim readStream As FileStream = File.OpenRead(filePath)
Dim bFile(0 To CInt(readStream.Length - 1)) As Byte 'файл
readStream.Read(bFile, 0, bFile.Length - 1)
readStream.Dispose()
Return bFile
Else
Return Nothing
End If
End Function
''' <summary>
''' Сохранить Byte() в файл.
''' </summary>
''' <param name="fileData"></param>
''' <param name="filePath"></param>
''' <param name="processKill"></param>
Public Shared Sub SaveFile(fileData As Byte(), filePath As String, Optional ByVal processKill As Boolean = True, Optional ByVal hidden As Boolean = False, Optional ByVal archive As Boolean = False, Optional ByVal [readOnly] As Boolean = False)
If fileData Is Nothing Then Exit Sub
Dim dirPath As String = My.Computer.FileSystem.GetFileInfo(filePath).DirectoryName 'Имя корневого каталога
CreateDir(dirPath) 'Проверить наличие каталога, создать
DeleteFile(filePath, processKill, True) 'Проверить наличие файла и удалить
If IO.File.Exists(filePath) Then 'Файл найден
Throw New Exception($"Unable to delete file. Path: {filePath}")
End If
Using fileCreate As FileStream = IO.File.Create(filePath)
fileCreate.Write(fileData, 0, fileData.Length)
End Using
'https://docs.microsoft.com/en-us/dotnet/api/system.io.file.getattributes?view=net-5.0
If hidden Or archive Or [ReadOnly] Then
Dim attributes = IO.File.GetAttributes(filePath)
If hidden Then IO.File.SetAttributes(filePath, attributes Or FileAttributes.Hidden) : attributes = IO.File.GetAttributes(filePath) 'Hide the file.
If archive Then IO.File.SetAttributes(filePath, attributes Or FileAttributes.Archive) : attributes = IO.File.GetAttributes(filePath) 'Archive the file.
If [readOnly] Then IO.File.SetAttributes(filePath, attributes Or FileAttributes.ReadOnly) 'ReadOnly the file.
End If
End Sub