Simple Python Challenge: Fastest Bitwise XOR on Data Buffers

Posted by user213060 on Stack Overflow See other posts from Stack Overflow or by user213060
Published on 2010-01-22T19:08:49Z Indexed on 2010/04/02 10:23 UTC
Read the original article Hit count: 319

Challenge:

Perform a bitwise XOR on two equal sized buffers. The buffers will be required to be the python str type since this is traditionally the type for data buffers in python. Return the resultant value as a str. Do this as fast as possible.

The inputs are two 1 megabyte (2**20 byte) strings.

The challenge is to substantially beat my inefficient algorithm using python or existing third party python modules (relaxed rules: or create your own module.) Marginal increases are useless.

from os import urandom
from numpy import frombuffer,bitwise_xor,byte

def slow_xor(aa,bb):
    a=frombuffer(aa,dtype=byte)
    b=frombuffer(bb,dtype=byte)
    c=bitwise_xor(a,b)
    r=c.tostring()
    return r

aa=urandom(2**20)
bb=urandom(2**20)

def test_it():
    for x in xrange(1000):
        slow_xor(aa,bb)

© Stack Overflow or respective owner

Related posts about xor

Related posts about algorithm