<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=windows-1252">
<META content="MSHTML 6.00.6000.16608" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=#ffffff>
<DIV><FONT color=#000080>Kent</FONT></DIV>
<DIV><FONT color=#000080></FONT>&nbsp;</DIV>
<DIV><FONT color=#000080>I'm using a Javascript autocomplete plugin for an 
online web application/service.&nbsp; Each time a user inputs a character, the 
character is sent to the backend Python program which searches for the character 
in a list of &gt;10,000 string items.&nbsp; Once it finds the character, the 
backend will return that string and N other adjacent string items where N can 
vary from 20 to 150.&nbsp; Each string item is sent back to the JS in separate 
print statements.&nbsp; Hence, the for loop.</FONT></DIV>
<DIV><FONT color=#000080></FONT>&nbsp;</DIV>
<DIV><FONT color=#000080>Now, N = 20 to 150 is not a lot (for a for loop) but 
this process is performed each time the user enters a character.&nbsp; Plus, 
there will be thousands (possibly more) users at a time.&nbsp; There is also the 
searching of the &gt;10,000 string items using the entered character.&nbsp; All 
of this adds up in terms of performance.</FONT></DIV>
<DIV><FONT color=#000080></FONT>&nbsp;</DIV>
<DIV><FONT color=#000080>I haven't done any profiling yet as we are still 
building the system but it seemed sensible that replacing the for loop with a 
built-in would help.&nbsp; Maybe not?</FONT></DIV>
<DIV><FONT color=#000080></FONT>&nbsp;</DIV>
<DIV><FONT color=#000080>Hope that helps.</FONT></DIV>
<DIV><FONT color=#000080></FONT>&nbsp;</DIV>
<DIV><FONT color=#000080>Dinesh</FONT></DIV>
<DIV><FONT color=#000080></FONT>&nbsp;</DIV>
<DIV><FONT color=#000080></FONT>&nbsp;</DIV>
<DIV style="FONT: 10pt arial">----- Original Message ----- 
<DIV style="BACKGROUND: #e4e4e4; font-color: black"><B>From:</B> <A 
title=kent37@tds.net href="mailto:kent37@tds.net">Kent Johnson</A> </DIV>
<DIV><B>To:</B> <A title=dineshbvadhia@hotmail.com 
href="mailto:dineshbvadhia@hotmail.com">Dinesh B Vadhia</A> </DIV>
<DIV><B>Cc:</B> <A title=tutor@python.org 
href="mailto:tutor@python.org">tutor@python.org</A> </DIV>
<DIV><B>Sent:</B> Wednesday, April 09, 2008 1:48 PM</DIV>
<DIV><B>Subject:</B> Re: [Tutor] List comprehensions</DIV></DIV>
<DIV><BR></DIV>Dinesh B Vadhia wrote:<BR>&gt; Here is a for loop operating on a 
list of string items:<BR>&gt;&nbsp; <BR>&gt; data = ["string 1", "string 2", 
"string 3", "string 4", "string 5", <BR>&gt; "string 6", "string 7", "string 8", 
"string 9", "string 10", "string 11"]<BR>&gt;&nbsp; <BR>&gt; result = ""<BR>&gt; 
for item in data:<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp; result = &lt;some operation 
on&gt; item<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp; print result<BR>&gt;&nbsp; <BR>&gt; 
I want to replace the for loop with another structure to improve <BR>&gt; 
performance (as the data list will contain &gt;10,000 string items].&nbsp; At 
<BR>&gt; each iteration of the for loop the result is printed (in fact, the 
<BR>&gt; result is sent from the server to a browser one result line at a 
time)<BR><BR>Any savings you have from optimizing this loop will be completely 
<BR>swamped by the network time. Why do you think this is a 
bottleneck?<BR><BR>You could use<BR>[ sys.stdout.write(some operation on item) 
for item in data ]<BR><BR>but I consider this bad style and I seriously doubt 
you will see any <BR>difference in performance.<BR><BR>&gt; The for loop will be 
called continuously and this is another reason to <BR>&gt; look for a 
potentially better structure preferably a built-in.<BR><BR>What do you mean 
'called continuously'?<BR><BR>Kent<BR></BODY></HTML>