[Gambas-user] My quest for efficiency
Fernando Cabral
fernandojosecabral at ...626...
Sun Jul 16 14:13:39 CEST 2017
Thank you, Caveat [emptor?].
The code you proposed worked very, very well.
In fact, I timed it against two versions of the C program and the result
was quite good.
In C, reading from the standard input and writing to the standard output
took a trifle beyond half a second (0.6?? real time).
Meanwhile, your version in Gambas ran in 2.5?? (real time). The question
mark means little variation from trial to trial.
Out of curiosity, I wrote a Gambas version similar to the C version. Here
are the two codes:
*Dim b As Byte*
* While Not Eof() b = Read As Byte Write (b + 11) As Byte Wend*
- - - - - - -
* int c; while((c = getchar()) != EOF) putchar(c + 11);*
The C version ran in 0.59 against the Gambas version that ran in 7.6
seconds (real time). Not too bad in my opinion!
Then I tried to stretch it a little bit and wrote:
* Write (( Read As Byte) + 11) As Byte*
Alas! this is something Gambas does not understand.
These are just for the sake of experience. I am happy with the solution
Caveat proposed.
Thank you.
2017-07-15 17:28 GMT-03:00 Caveat <Gambas at ...3670...>:
> 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,
>>
>>
>
>
> ------------------------------------------------------------
> ------------------
> Check out the vibrant tech community on one of the world's most
> engaging tech sites, Slashdot.org! http://sdm.link/slashdot
> _______________________________________________
> Gambas-user mailing list
> Gambas-user at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/gambas-user
>
--
Fernando Cabral
Blogue: http://fernandocabral.org
Twitter: http://twitter.com/fjcabral
e-mail: fernandojosecabral at ...626...
Facebook: f at ...3654...
Telegram: +55 (37) 99988-8868
Wickr ID: fernandocabral
WhatsApp: +55 (37) 99988-8868
Skype: fernandojosecabral
Telefone fixo: +55 (37) 3521-2183
Telefone celular: +55 (37) 99988-8868
Enquanto houver no mundo uma só pessoa sem casa ou sem alimentos,
nenhum político ou cientista poderá se gabar de nada.
More information about the User
mailing list