PyPK wrote:

> I was testing this piece of code and it takes about 24-30 seconds to do
> a look up on a list(m) of size 1000x1000
> m -> list of size 1000x1000
> import time
> print time.ctime()
> box = {}
> for r,row in enumerate(m):
>     for c,e in enumerate(row):
>          if box.has_key(e):
>                params = box[e]
>                box[e] = ( min(c, params[0]), min(r, params[1]),
>                           max(c, params[2]), max(r, params[3] ) )
>           else:
>                 box[e] = [c, r, c, r]
> print time.ctime()
> 
> Can some one tell me what exactly is taking more time here. Is it
> because I am using dictionaries or what is the problem. Can some one
> help me improve this .Is there a better way to write this.

AFAICT the row index 'r' can only grow. Therefore one min() and one max()
should be superfluous (untested):

def make_box(m):
    box = {}
    for r, row in enumerate(m):
        for c, e in enumerate(row):
            if e in box:
                left, top, right, bottom  = box[e]
                box[e] = (min(c, left), top, max(c, right), r)
            else:
                box[e] = (c, r, c, r)
    return box

Peter
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to