[Checkins] SVN: zc.table/trunk/src/zc/table/column.py Speed up column sorting (helps a lot when 10k of items are involved,

Benji York benji at zope.com
Mon Oct 20 15:51:48 EDT 2008


On Mon, Oct 20, 2008 at 11:49 AM, Ignas Mikalajūnas <ignas at pov.lt> wrote:
> Log message for revision 92406:
>  Speed up column sorting (helps a lot when 10k of items are involved,
>  especially when you have a moderately expensive getSortKey function)

Wouldn't it be slightly faster and a little simpler to use sort's "key"
parameter?

> Changed:
>  U   zc.table/trunk/src/zc/table/column.py
>
> -=-
> Modified: zc.table/trunk/src/zc/table/column.py
> ===================================================================
> --- zc.table/trunk/src/zc/table/column.py       2008-10-20 15:19:25 UTC (rev 92405)
> +++ zc.table/trunk/src/zc/table/column.py       2008-10-20 15:49:38 UTC (rev 92406)
> @@ -59,11 +59,13 @@
>         else:
>             items = list(items) # don't mutate original
>         getSortKey = self.getSortKey
> -        items.sort(
> -            lambda a, b: multiplier*cmp(getSortKey(a, formatter),
> -                                        getSortKey(b, formatter)))
> -        return items
>
> +        # let's do decorate, sort, undecorate trick here to conserve time
> +        tmp_items = [(getSortKey(item, formatter), item) for item in items]
> +        tmp_items.sort(lambda a, b: multiplier*cmp(a[0], b[0]))
> +
> +        return [item for key, item in tmp_items]
> +
>     def sort(self, items, formatter, start, stop, sorters):
>         return self._sort(items, formatter, start, stop, sorters, 1)

-- 
Benji York
Senior Software Engineer
Zope Corporation


More information about the Checkins mailing list