[Gambas-user] My quest for efficiency
Caveat
Gambas at ...3662...
Sat Jul 15 22:28:38 CEST 2017
Something is horribly wrong, or you're running on a 286 :-)
I just tested here, and the program runs on a 51 MB test file in about 5
seconds.
Some reasonably well commented code for you...
Public Sub Main()
Dim inFile, outFile As File
Dim buff As New Byte[1024]
Dim idx, remBytes, readSize As Integer
' CHANGE THIS to your input file
inFile = Open "/home/caveat/Downloads/mytestfile" For Read
' CHANGE THIS to your output file
outFile = Open "/home/caveat/Downloads/mytestfile.out2" For Create
' Remaining bytes starts as the total length of the file
remBytes = Lof(inFile)
' Until we reach the end of the input file...guess you could instead
check on remBytes...
While Not Eof(inFile)
If remBytes > buff.length Then
' Limit reading to the size of our buffer (the Byte[])
readSize = buff.length
Else
' Only read the bytes we have left into our buffer (the Byte[])
readSize = remBytes
Endif
' Read from the input file into our buffer, starting at offset 0 in
the buffer
buff.Read(inFile, 0, readSize)
' Update the number of bytes remaining...
remBytes = remBytes - readSize
' Run round each byte in our buffer
For idx = 0 To buff.length - 1
' Dunno if you need any conditions, I check for > 30 as I can put
newlines in the file to make it more readable for testing
If buff[idx] > 30 Then
' This is the 'trick' you need to apply... subtract 11 from
every byte in the file
' Not sure how you deal with edge cases... if you have a byte
of 5, is your result then 250?
buff[idx] = buff[idx] - 11
Endif
Next
' Write the whole buffer out to the output file
buff.Write(outFile, 0, readSize)
Wend
Close #inFile
Close #outFile
End
Kind regards,
Caveat
On 15-07-17 21:24, Benoît Minisini via Gambas-user wrote:
> Le 15/07/2017 à 20:49, Tony Morehen a écrit :
>> Did you try Benoit's suggestion:
>>
>> Public Sub Main()
>>
>> Dim sIn as String
>> Dim sOut as String
>>
>> sIn = File.Load("/home/fernando/temp/deah001.dhn")
>> sOut = Add11(sIn)
>> File.Save("/home/fernando/temp/deah001.11Added.dhn", sOut)
>>
>> End
>>
>> Public Sub Add11(InputString as String) as String
>> Dim bArray As Byte[]
>> Dim String11 As String
>> Dim i As Integer
>>
>> bArray = Byte[].FromString(InputString)
>> For i = 0 To bArray.Max
>> bArray[i] += 11
>> Next
>> Return bArray.ToString
>> End
>>
>>
>
> Just a remark:
>
> You don't have to use Byte[].FromString.
>
> You can use the Bute[].Read() method instead, to load the file
> directly into the array. You save an intermediate string that way.
>
> Regards,
>
More information about the User
mailing list