Dinesh B Vadhia wrote:
> Kent
>  
> I'm using a Javascript autocomplete plugin for an online web 
> application/service.  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 >10,000 string items. 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.
So if I had these strings

"ape", "bee", "cat", dog", "eel", "fly", "gnu", "hex", "imp", "jut", 
"kit", "lox"

and N were 2 and the user entered "g" the program finds "dog" and sends 
back "cat", dog", "eel".

OK so far or not?

The user then enters "y", the program finds "fly" and sends back "eel", 
"fly", "gnu".

OK so far or not?

The user then enters "x", the program finds no match and sends back what??

>  Each string item is sent back to the JS in separate print statements. 
IIRC you don't need separate print statements. Just put \n between the 
strings and print one big string.
> Hence, the for loop.
>  
> 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.  Plus, there will be 
> thousands (possibly more) users at a time.  There is also the 
> searching of the >10,000 string items using the entered character.  
> All of this adds up in terms of performance.
>  
> 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.  Maybe not?
>  
> Hope that helps.
>  
> Dinesh
>  
>  
> ----- Original Message -----
> *From:* Kent Johnson <mailto:[EMAIL PROTECTED]>
> *To:* Dinesh B Vadhia <mailto:[EMAIL PROTECTED]>
> *Cc:* tutor@python.org <mailto:tutor@python.org>
> *Sent:* Wednesday, April 09, 2008 1:48 PM
> *Subject:* Re: [Tutor] List comprehensions
>
> Dinesh B Vadhia wrote:
> > Here is a for loop operating on a list of string items:
> > 
> > data = ["string 1", "string 2", "string 3", "string 4", "string 5",
> > "string 6", "string 7", "string 8", "string 9", "string 10", "string 
> 11"]
> > 
> > result = ""
> > for item in data:
> >     result = <some operation on> item
> >     print result
> > 
> > I want to replace the for loop with another structure to improve
> > performance (as the data list will contain >10,000 string items].  At
> > each iteration of the for loop the result is printed (in fact, the
> > result is sent from the server to a browser one result line at a time)
>
> Any savings you have from optimizing this loop will be completely
> swamped by the network time. Why do you think this is a bottleneck?
>
> You could use
> [ sys.stdout.write(some operation on item) for item in data ]
>
> but I consider this bad style and I seriously doubt you will see any
> difference in performance.
>
> > The for loop will be called continuously and this is another reason to
> > look for a potentially better structure preferably a built-in.
>
> What do you mean 'called continuously'?
>
> Kent
> ------------------------------------------------------------------------
>
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>   


-- 
Bob Gailer
919-636-4239 Chapel Hill, NC

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to