Simple Python Challenge Fastest Bitwise XOR on Data Buffers
Data Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.
Introduction
XORing two data buffers byte-by-byte in pure Python is slow because of per-byte loop overhead. The fastest approaches convert buffers to Python int (which supports arbitrary-width XOR natively), use NumPy's vectorized operations, or leverage bytes with int.from_bytes/int.to_bytes. For large buffers, NumPy is 100-1000x faster than a Python for-loop.
The Naive Approach (Slow)
This creates a generator that XORs one byte at a time. Python's per-iteration overhead makes this O(n) with a large constant factor.
Method 1: int.from_bytes XOR (Fast, No Dependencies)
Convert both buffers to a single large integer, XOR them, and convert back:
Python's arbitrary-precision integer XOR is implemented in C and operates on machine words (8 bytes at a time), making it much faster than byte-by-byte iteration.
Method 2: NumPy (Fastest for Large Buffers)
NumPy uses SIMD (Single Instruction Multiple Data) instructions to XOR 16-64 bytes per CPU instruction. This is the fastest option for buffers over a few kilobytes.
Method 3: bytearray with memoryview
Slightly faster than the generator approach because bytearray avoids creating intermediate tuples, but still limited by Python's loop overhead.
Method 4: Using struct for Word-Aligned XOR
This processes 8 bytes per iteration by unpacking to 64-bit unsigned integers. Faster than byte-by-byte but slower than int.from_bytes or NumPy.
Method 5: ctypes / cffi for C-Level Speed
Still limited by Python loop overhead. For true C-level performance, write a C extension or use Cython:
Benchmarks
Typical results for 100 KB buffers (100 iterations):
| Method | Time (s) | Relative Speed |
| Naive (generator) | 3.2 | 1x |
| bytearray loop | 2.5 | 1.3x |
| struct (8B chunks) | 0.8 | 4x |
| int.from_bytes | 0.05 | 64x |
| NumPy | 0.02 | 160x |
Practical Use Cases
Common Pitfalls
- Different buffer lengths: XOR requires equal-length inputs.
zip()silently truncates to the shorter buffer. Always check lengths first or pad the shorter buffer. int.from_byteswith empty buffers:int.from_bytes(b'', 'big')returns0, and(0).to_bytes(0, 'big')returnsb''. This is correct but can be confusing.- NumPy overhead for small buffers: NumPy has fixed overhead for array creation. For buffers under 100 bytes, the naive approach or
int.from_bytesmay be faster. - Mutability:
bytesis immutable. If you need to XOR in-place, usebytearrayor NumPy arrays withnp.bitwise_xor(arr1, arr2, out=arr1). - Memory usage:
int.from_bytescreates a Python int that uses roughly the same memory as the buffer. NumPy arrays also copy by default. For multi-gigabyte buffers, usenp.frombuffer(zero-copy view) and process in chunks.
Summary
- Naive byte-by-byte XOR in Python is 100-1000x slower than optimized approaches
int.from_bytes+^+to_bytesis the fastest pure-Python method (no dependencies)- NumPy is the fastest overall — uses SIMD instructions for vectorized XOR
- For small buffers (under 100 bytes), the overhead of NumPy or int conversion may exceed the loop savings
- Use
struct.unpackfor moderate speedup without external dependencies - For production crypto or network code, use libraries like
cryptographythat implement XOR in C
Related reading
- Simple Python implementation of collaborative topic modeling?
- Simple way to find if two different lists contain exactly the same elements?
- Simple way to measure cell execution time in ipython notebook
- Simpler way to avoid the UserWarning Converting sparse IndexedSlices
- Simpler way to create dictionary of separate variables?
- Simplest async/await example possible in Python
- Simplest async/await example possible in Python
- Simplest way to form a union of two lists
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.