Hi, 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 Thus I suppose that the xor operation between bytes raise the execution time to 0.5; why I suppose that? Because in Python there's no support for bytes and even for xoring bytes, so I used a workaround: I cycle on the two strings to be xor-red for every char in the strings convert one char on integer and then xor them; (ord) insert one char in the result, transforming the previous integer in char (chr) I suppose that ord() and char() are the main problems of my implementation, but I don't know either way to xor two bytes of data (bytes are represented as strings). For more information, see the code attached. How should I decrease the execution time? Thank you from __future__ import division import random import time import sha import os class Encoder(object): def create_random_block(self, data, seed, blocksize): number_of_blocks = int(len(data)/blocksize) random.seed(seed) random_block = ['0'] * blocksize for index in range(number_of_blocks): if int(random.getrandbits(1)) == 1: block = data[blocksize*index:blocksize*index+blocksize] for bit in range(len(block)): random_block[bit] = chr(ord(random_block[bit])^ord(block[bit])) # workaround per fare xor bit a bit di str; xor e' solo supportato per int -> ord return ''.join(random_block) x = Encoder() piece = os.urandom(1024*1024) blocksize = 16384 t1 = time.time() for l in range(100): seed = random.getrandbits(32) block = x.create_random_block(piece, seed, blocksize) t2 = time.time() print 'took ' + str(t2-t1) + 's, avg: ' + str((t2-t1)/100.0) + 's' -- http://mail.python.org/mailman/listinfo/python-list