On Jul 13, 3:46 pm, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote: > On Jul 13, 5:17 am, Paul McGuire <[EMAIL PROTECTED]> wrote: > > > > > > > On Jul 12, 5:34 pm, Godzilla <[EMAIL PROTECTED]> wrote: > > > > Hello, > > > > I'm trying to find a way to convert an integer (8-bits long for > > > starters) and converting them to a list, e.g.: > > > > num = 255 > > > numList = [1,1,1,1,1,1,1,1] > > > > with the first element of the list being the least significant, so > > > that i can keep appending to that list without having to worry about > > > the size of the integer. I need to do this because some of the > > > function call can return a 2 lots of 32-bit numbers. I have to find a > > > way to transport this in a list... or is there a better way? > > > Standing on the shoulders of previous posters, I put this together. > > > -- Paul > > But aren't we moving backwards? The OP did ask for the fastest way. > > I put this together (from other posters and my own): > > import gmpy > import time > > y = 2**177149 - 1 > > # init list of tuples by byte > bytebits = lambda num : [num >> i & 1 for i in range(8)] > bytes = [ tuple(bytebits(i)) for i in range(256) ] > # use bytes lookup to get bits in a 32-bit integer > bits = lambda num : sum((bytes[num >> i & 255] for i in range(0,32,8)), > ()) > # use base-2 log to find how many bits in an integer of arbitrary > length > from math import log,ceil > log_of_2 = log(2) > numBits = lambda num : int(ceil(log(num)/log_of_2)) > # expand bits to integers of arbitrary length > arbBits = lambda num : sum((bytes[num >> i & 255] for i in > range(0,numBits(num),8)),()) > t0 = time.time() > L = arbBits(y) > t1 = time.time() > print 'Paul McGuire algorithm:',t1-t0 > > t0 = time.time() > L = [y >> i & 1 for i in range(177149)] > t1 = time.time() > print ' Matimus algorithm:',t1-t0 > > x = gmpy.mpz(2**177149 - 1) > t0 = time.time() > L = [gmpy.getbit(x,i) for i in range(177149)] > t1 = time.time() > print ' Mensanator algorithm:',t1-t0 > > ## Paul McGuire algorithm: 17.4839999676 > ## Matimus algorithm: 3.28100013733 > ## Mensanator algorithm: 0.125- Hide quoted text - > > - Show quoted text -
Oof! Pre-calculating those byte bitmasks doesn't help at all! It would seem it is faster to use a single list comp than to try to sum together the precalcuated sublists. I *would* say though that it is somewhat cheating to call the other two algorithms with the hardcoded range length of 177149, when you know this is the right range because this is tailored to fit the input value 2**177149-1. This would be a horrible value to use if the input number were something small, like 5. I think numBits still helps here to handle integers of arbitrary length (and only adds a slight performance penalty since it is called only once). -- Paul -- http://mail.python.org/mailman/listinfo/python-list