Steven D'Aprano <[EMAIL PROTECTED]> writes: > For a short string like "Python", using an array is a tiny bit slower, at > the cost of more complex code.... if you're converting a long string, > using array is faster. If it is a short string, it doesn't make much > difference.
I modified your array version slightly: def flip3(text): n = len(text) mask = ord('U') text = array('b', text) for i in xrange(n): text[i] ^= mask return text.tostring() and I got flip3("Python") a little faster than the listcomp version, but yeah, I was concerned mostly about long strings. For fixed-sized short strings, using array('l') and unrolling the loop makes a big difference: text = "Pythonic" mask = array('l','UUUU')[0] def flip4(text): text = array('l', text) text[0] ^= mask text[1] ^= mask return text.tostring() >>> timeit.Timer('flip1(text)', setup).repeat() # your version [35.932021141052246, 36.262560844421387, 40.019834041595459] >>> timeit.Timer('flip3(text)', setup).repeat() # flip3 above [33.44039511680603, 31.375681161880493, 31.374078035354614] >>> timeit.Timer('flip4(text)', setup).repeat() # flip4 above [15.349261045455933, 15.526498794555664, 15.351589202880859] See http://www.nightsong.com/phr/crypto/p3.py for an encryption routine written this way. -- http://mail.python.org/mailman/listinfo/python-list