Possible memory leak?

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Thu Jan 26 23:34:04 CET 2006


In <1138210684.619217.129660 at z14g2000cwz.googlegroups.com>, Tuvas wrote:

> The modified version of my code is now as follows: (Note, a few small
> changes have been made to simplify things, however, these things don't
> apply to a full-scale picture, so the shouldn't slow anything down in
> the slightest.)
> 
> def load_pic_data(width,heigth,inpdat, filt=TRUE):
>         ldata=[]
>         total=0
>         tnum=0
>         size=100
>         array=[]
>         for y in range(0,heigth):
>                 row=[]
>                 ts=time.time()
>                 for x in range(0,width):
>                         index=2*(x+y*width)
>                         num=ord(inpdat[index+1])*256+ord(inpdat[index])
>                         if(vfilter.get() and d_filter and filt):
>                                 num=round((num-(d_filter[index/2])))
>                         if(num<0):
>                                 num=0
>                         if(num>255*64):
>                                 num=255*64
>                         tba=chr(num/64)
>                         row.append(tba)
>                 srow=''.join(row)
>                 ldata.append(srow)
>                 print y,time.time()-ts
>         data=''.join(ldata)

Do you always work on the whole `width` and `height` of the picture?  If
yes, do you really need the `x` and `y` coordinate?  The snipped above
would work if you just process all values as one sequence.

And using `array.array` for `inpdat` and the result may speed things up
too::

	inpdat_array = array('H', inpdat)
        data = array('c')
        for index in xrange(width * heigth):
            num = inpdat_array[index]
            if vfilter.get() and d_filter and filt:
                num = round(num - d_filter[index])
            if num < 0:
                num = 0
            if num > 255 * 64:
                num = 255 * 64
            data.append(num // 64)
        return data.tostring()

If you don't need the `index` somewhere in the loop you can even get rid
of it by always subtract the filter value and supply a "zero filter" if
the condition of the first ``if`` is not fulfilled::

        from itertools import izip, repeat
        
        inpdat_array = array('H', inpdat)
        data = array('c')
        
        if not (vfilter.get() and d_filter and filt):
            filter_values = repeat(0)
        else:
            filter_values = d_filter
        
        for num, filter_value in izip(inpdat_array, filter_values):
            num = round(inpdat_array[index] - filter_value)
            if num < 0:
                num = 0
            if num > 255 * 64:
                num = 255 * 64
            data.append(num // 64)
        return data.tostring()

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list