XORing long strings opimization?

Noen not.available at na.no
Wed Nov 5 00:26:51 CET 2003

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

More information about the Python-list mailing list