Convert to binary and convert back to strings

Steven D'Aprano steve at REMOVEME.cybersource.com.au
Thu Feb 22 22:09:44 EST 2007


On Thu, 22 Feb 2007 14:29:13 -0800, Paul Rubin wrote:

> Steven D'Aprano <steve at REMOVE.THIS.cybersource.com.au> writes:
>> Arrays don't support XOR any more than strings do. What's the advantage to
>> using the array module if you still have to jump through hoops to get it
>> to work?
> 
> It's a lot faster.  Sometimes that matters.

But is it?

>>> import array
>>> import timeit
>>>
>>> def flip1(text):
...     mask = ord('U')
...     return ''.join([chr(ord(c) ^ mask) for c in text])
...
>>> def flip2(text):
...     mask = ord('U')
...     text = array.array('b', [b ^ mask for b in array.array('b',text)])
...     return text.tostring()
... 
>>> text = "Python"
>>> setup = 'from __main__ import flip1, flip2, text'
>>> 
>>> timeit.Timer('flip1(text)', setup).repeat()
[25.757978916168213, 23.174431085586548, 23.188597917556763]
>>> timeit.Timer('flip2(text)', setup).repeat()
[25.736327886581421, 25.76999306678772, 26.135013818740845]


For a short string like "Python", using an array is a tiny bit slower, at
the cost of more complex code.

What about for a long string?

>>> text = 'Python'*1000
>>> 
>>> timeit.Timer('flip1(text)', setup).repeat(3, 2000)
[24.483185052871704, 26.007826089859009, 24.498424053192139]
>>> timeit.Timer('flip2(text)', setup).repeat(3, 2000)
[12.18204402923584, 12.342558860778809, 12.16040301322937]

Well, that answers that question -- if you're converting a long string,
using array is faster. If it is a short string, it doesn't make much
difference.



-- 
Steven D'Aprano 




More information about the Python-list mailing list