473,699 Members | 2,386 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Optimization of __len__() in cgi.py

I have a suggestion for speeding up the performance of code like this:

fields = cgi.FieldStorag e()
if fields: ...

which, as it turns out, invokes FieldStorage.__ len__(), which in turn
calls FieldStorage.ke ys(), 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
Aug 16 '06 #1
5 1369
In <Cs************ *************** ***@speakeasy.n et>, Bob Kline wrote:
I have a suggestion for speeding up the performance of code like this:

fields = cgi.FieldStorag e()
if fields: ...

which, as it turns out, invokes FieldStorage.__ len__(), which in turn
calls FieldStorage.ke ys(), 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.n ame 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
Aug 16 '06 #2
Bob Kline wrote:
I have a suggestion for speeding up the performance of code like this:

fields = cgi.FieldStorag e()
if fields: ...

which, as it turns out, invokes FieldStorage.__ len__(), which in turn
calls FieldStorage.ke ys(), 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?
Post a RFE to the Python Tracker at
http://sourceforge.net/tracker/?grou...70&atid=355470

If you want, assign it to me (gbrandl).

Georg
Aug 16 '06 #3
Marc 'BlackJack' Rintsch wrote:
> 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.
Such code would be flying in the face of an implication that the order
of the keys is *not* preserved, as the keys() method is documented as
"Dictionary style keys() method" (and the documentation for dictionary
keys() says "... listed in an arbitrary order which is non-random ...").

If it were decided that the behavior of keys() should be preserved even
to the extent of preserving the order of the first occurrence of each
field name, then the optimization could just be moved to __len__():

def __len__(self):
return len({}.fromkeys ([i.name for i 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.
This approach also risks breaking code that assumes it's getting the
number of unique field names (an assumption which in this case would be
justified, given the documentation of the keys() method quoted above).

Bob
Aug 16 '06 #4
Sybren Stuvel wrote:
FieldStorage.__ nonzero__ tried first if it exists. You might want to
use that for more optimization.
Excellent suggestion! It would be nice if this were adopted to
supplement the original optimization, rather than replace it.

Bob
Aug 16 '06 #5
Georg Brandl wrote:
Post a RFE to the Python Tracker at
http://sourceforge.net/tracker/?grou...70&atid=355470

If you want, assign it to me (gbrandl).
Done, thanks.

Bob
Aug 16 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
1871
by: george young | last post by:
I had developed the habit of using the neat python form: if someinstance: someinstance.memb() because it seems cleaner than "if someinstance is not None". {please no flames about "is not None" vs. "!= None" ...} This seemed like a good idea at the time :(). Twice, recently, however, as my app grew, I thought, hmm... it would make things clearer if I gave
3
12118
by: MackS | last post by:
I'm new to Python. In general I manage to understand what is happening when things go wrong. However, the small program I am writing now fails with the following message: AttributeError: ClassA instance has no attribute '__len__' Following the traceback,I see that the offending line is self.x = arg1 + len(self.y) + 1
9
2395
by: Rune | last post by:
Is it best to use double quotes and let PHP expand variables inside strings, or is it faster to do the string manipulation yourself manually? Which is quicker? 1) $insert = 'To Be'; $sentence = "$insert or not $insert. That is the question."; or
12
6189
by: WantedToBeDBA | last post by:
Hi all, db2 => create table emp(empno int not null primary key, \ db2 (cont.) => sex char(1) not null constraint s_check check \ db2 (cont.) => (sex in ('m','f')) \ db2 (cont.) => not enforced \ db2 (cont.) => enable query optimization) DB20000I The SQL command completed successfully. db2 => insert into emp values(1,'m')
21
2570
by: mjbackues at yahoo | last post by:
Hello. I'm having a problem with the Visual Studio .net (2003) C++ speed optimization, and hope someone can suggest a workaround. My project includes many C++ files, most of which work fine with speed optimization turned on. At least one does not however, though it does work with size optimization turned on. I don't know specifically what the optimizer is doing wrong, just that the output is incorrect. And I know within about 10...
5
2387
by: wkaras | last post by:
I've compiled this code: const int x0 = 10; const int x1 = 20; const int x2 = 30; int x = { x2, x0, x1 }; struct Y {
2
2177
by: sndive | last post by:
i extract class object from an instance of NodeList (minicompat.py) like so PyObject *pclass = PyObject_GetAttrString( nodelistinsance, "__class__"); but PyObject_GetAttrString(pclass, "__len__") returns NULL. i naively added __len__ = _get_length
10
6554
by: Nikhil | last post by:
which one is better? and why? __len__() is a built-in function of the list object and is updated along with the list object elements and will be useful incase the list is very huge. len() is an external method again, which may require the processing cycles again. Is it right?
20
2340
by: Ravikiran | last post by:
Hi Friends, I wanted know about whatt is ment by zero optimization and sign optimization and its differences.... Thank you...
0
8685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9172
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9032
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8908
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6532
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5869
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4374
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.