xor: how come so slow?
Peter Otten
__peter__ at web.de
Wed Oct 15 08:58:16 EDT 2008
Michele wrote:
> I'm trying to encode a byte data. Let's not focus on the process of
> encoding; in fact, I want to emphasize that the method
> create_random_block takes 0.5s to be executed (even Java it's faster) on
> a Dual-Core 3.0Ghz machine:
>
> took 46.746999979s, avg: 0.46746999979s
> How should I decrease the execution time?
Use numpy. You should be able to swap in the following in your script
import numpy
from numpy.core import multiarray as ma
class Encoder(object):
def create_random_block(self, data, seed, blocksize):
number_of_blocks = len(data)//blocksize
random.seed(seed)
random_block = ma.fromstring("0"*blocksize, numpy.uint8)
for index in range(number_of_blocks):
if random.getrandbits(1):
block =
ma.fromstring(data[blocksize*index:blocksize*index+blocksize], numpy.uint8)
random_block ^= block
return random_block.tostring()
There are absolutely no warranties as I'm just a random tinkerer when it
comes to numpy. But if it works you should get a nice speedup...
Peter
More information about the Python-list
mailing list