Optimization of __len__() in cgi.py

Bob Kline bkline at rksystems.com
Wed Aug 16 08:50:46 EDT 2006


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()

Is there a better place for submitting suggestions like this?

Bob Kline



More information about the Python-list mailing list