XORing long strings opimization?
Casey Whitelaw
casey at caseyporn.com
Tue Nov 4 23:19:41 EST 2003
Noen <not.available at na.no> wrote in message news:<%AWpb.24470$BD3.4569022 at juliett.dax.net>...
> Here is the result, if anyone wants it :)
> It was quite quick indeed, It took me 10.3279999495 seconds to XOR
> "a"*1000000 large string. Im going to XOR 700000000 mb sized files, so
> if anyone know how to make it even faster, please tell me :)
>
> import string
> def xorstring(s1,s2):
> """ XOR string s1 with s2 """
> # Argument check
> if not (type(s1) == type("")) or not (type(s2) == type("")):
> raise TypeError, "Arguments are not strings"
> if len(s1) != len(s2):
> raise ValueError, "Size differs in strings"
> # Xoring
>
> # Create lists
> l1 = map(ord, s1)
> l2 = map(ord, s2)
>
> # Xor it all together
> xorlist = []
> xorlist = [chr(x ^ y) for (x, y) in zip(l1, l2)]
> return string.join(xorlist,"") # Backward compatible
Just fiddling around with the (excellent) suggestions already posted,
and found a couple of things.
Alex's suggestion using array is the fastest, but was slightly faster
when using xrange() rather than enumerate():
def xor6(s1, s2):
a1 = array.array("b", s1)
a2 = array.array("b", s2)
for i in xrange(len(a1)):
a1[i] ^= a2[i]
return a1.tostring()
On my system, this XORs 500,000 characters in 0.39s, with Noen's
original taking 0.67s.
Using psyco produced a further ~3x speedup, reducing this to 0.14s.
That's pretty fast!
Casey
More information about the Python-list
mailing list