Newbie looking for elegant solution

Rustom Mody rustompmody at gmail.com
Wed Mar 25 02:32:54 EDT 2015


On Wednesday, March 25, 2015 at 11:23:08 AM UTC+5:30, Paul Rubin wrote:
> kai.peters  writes
> > 1 bit images of a size of 1024 x 1280 need to be processed this way,
> > so 1310720 list elements. Also needs to be 2.7 only.
> 
> Where are these lists going to come from?  Files?  Process the file
> differently, probably.  Use generators instead of lists, maybe.  

Some C-ish solutions and then two loop-unrollings:

def foo(lst):
    i = 0
    while i < len(lst):
        acc = 0
        for j in range(8):
            acc = 2*acc+lst[i]
            i += 1
        yield acc

def bar(lst):
    i = 0
    while i < len(lst):
        acc = 0
        acc = 2*acc+lst[i]
        acc = 2*acc+lst[i+1]
        acc = 2*acc+lst[i+2]
        acc = 2*acc+lst[i+3]
        acc = 2*acc+lst[i+4]
        acc = 2*acc+lst[i+5]
        acc = 2*acc+lst[i+6]
        acc = 2*acc+lst[i+7]
        i += 8
        yield acc

def baz(lst):
    i = 0
    while i < len(lst):
        acc = (128*lst[i] + 64*lst[i+1] + 32*lst[i+2] + 16*lst[i+3] +
               8*lst[i+4] + 4*lst[i+5] + 2*lst[i+6] + lst[i+7])
        i += 8
        yield acc



More information about the Python-list mailing list