[Gambas-user] My quest for efficiency

Benoît Minisini gambas at ...1...
Sat Jul 15 16:28:47 CEST 2017


Le 15/07/2017 à 16:08, Fernando Cabral a écrit :
> Hi
> 
> I've found a file whose text has been obfuscated by subtracting 11 from
> every byte. Now I want to bring it back to regular text. To do this I have
> to add 11 to each byte read from that file. Now, I have tried several ways
> to do it, and they all seemed every inefficient to me. Two examples follow
> 
> *j = 0For i = 0 To Len(RawText)str &= Chr(CByte(Asc(RawText, i) + 11))  '
> either this or the following'Mid(Rawtext, i, 1) = Chr(CByte(Asc(RawText, i)
> + 11))Inc jIf j = 100000 Then   Print i; Now   j = 0EndifNext*
> 
> In the first option (uncommented) I am building a new string byte by byte.
> In the second option (commented) I am replacing each character in place.
> I expected the second option to be way faster, especially because there is
> no need for the string to be reallocated. Nevertheless, it showed to be a
> snail.
> The first option, in spite of the fact that it grows slower and slower as
> the string grows, is still way faster than the second option.
> 
> 
> To me it does not make sense. Does it for you?
> Also, is there a faster way to do this?
> 

Strings in Gambas are immutable. The second method does not act "in 
place", because this syntactic sugar actually creates a new string by 
concatenating the left part, the new character, and the right part.

Use a byte array instead of a string (if you are sure that you are 
dealing with ASCII of course), or a string buffer (OPEN STRING syntax 
see the wiki for the details)

Regards,

-- 
Benoît Minisini




More information about the User mailing list