Optimization of __len__() in cgi.py

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Wed Aug 16 10:01:23 EDT 2006


In <CsKdnTskBYHSiX7ZnZ2dnUVZ_qydnZ2d at speakeasy.net>, Bob Kline wrote:

> I have a suggestion for speeding up the performance of code like this:
> 
> fields = cgi.FieldStorage()
> if fields: ...
> 
> which, as it turns out, invokes FieldStorage.__len__(), which in turn
> calls FieldStorage.keys(), which builds a list of keys by hand, taking
> several minutes for large forms.  This implementation of keys() reduces
> the amount of time taken by several orders of magnitude:
> 
>     def keys(self):
>         return {}.fromkeys([i.name for i in self.list]).keys()

This does not maintain the order of `self.list`.  Don't know if there's
code relying on this.

In a recent Python version one could use `set()` and a generator
expression::

    return list(set(item.name for item in self.list))

But maybe it's even faster to change `FieldStorage.__len__()` to return
the length of `self.list` directly without the need to create an
intermediate list that's thrown away immediately.

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list