To keep the casual prying eye at bay, I've been using the following XOR encryption on text files. It encrypts one character at a time and builds a new string from the encrypted characters.
I've used it only for small files (10K or less) so speed wasn't a factor. But last night I tried to encrypt a 1M file and after several minutes it was still running! I killed it.
I replaced the "temp$ = ..." line with the line that is commented out. The new code took only about 0.35 seconds. I guess it's not surprising that the new code was much faster, but I was very surprised at how slowly the original line of code worked.
It shows that the "temp$ = temp$ + " code is a poor choice for large strings. The timing results increased as the square of the string size.
It's a great example of how the choice of functions can significantly impact the speed of a program.
Anyone else have great examples of where simple changes made huge differences?
Code:
Function EncryptMyString(text$, Psw$) As String Local i As Long, a As Long, temp$, c$, result$ temp$ = text$ For i = 1 To Len(text$) a = i Mod Len(Psw$) If a = 0 Then a = Len(Psw$) temp$ = temp$ + Chr$(Asc(Mid$(Psw$,a,1)) Xor Asc(Mid$(text$,i,1))) ' Mid$(temp$,i) = (Chr$(Asc(Mid$(Psw$,a,1)) Xor Asc(Mid$(text$,i,1)))) Next i Function = temp$ End Function
I replaced the "temp$ = ..." line with the line that is commented out. The new code took only about 0.35 seconds. I guess it's not surprising that the new code was much faster, but I was very surprised at how slowly the original line of code worked.
It shows that the "temp$ = temp$ + " code is a poor choice for large strings. The timing results increased as the square of the string size.
It's a great example of how the choice of functions can significantly impact the speed of a program.
Anyone else have great examples of where simple changes made huge differences?
Comment