Connecting Tech Pros Worldwide Help | Site Map
 
 
LinkBack Thread Tools Search this Thread
  #1  
Old July 18th, 2005, 07:42 PM
Jan Gregor
Guest
 
Posts: n/a
Default jython and concatenation of strings

Hello

I found that price of += operator on string is too high in jython. For
example 5000 such operations took 90 seconds (i generated html copy of
table with 1000 rows and 5 columns). Generation of row data into separate
string and joining after lead to time 13 seconds !!!

What's alternative way to do that ? (similiar parts of my code are terribbly
slow and such simple solution as above didn't help).


Thanks,
Jan



  #2  
Old July 18th, 2005, 07:42 PM
Diez B. Roggisch
Guest
 
Posts: n/a
Default Re: jython and concatenation of strings

> I found that price of += operator on string is too high in jython. For[color=blue]
> example 5000 such operations took 90 seconds (i generated html copy of
> table with 1000 rows and 5 columns). Generation of row data into separate
> string and joining after lead to time 13 seconds !!![/color]

Its generally not recommended to use simple string concatenation for
building larger strings - neither in python nor in java/jython.

afaik there are two solutions to this: The java-way and the jython way:

java: Use StringBuffer
python: use a list, and append the strings to that. Then, when you want the
result, do

"".join(stringlist)

--
Regards,

Diez B. Roggisch
  #3  
Old July 18th, 2005, 07:42 PM
Steven Bethard
Guest
 
Posts: n/a
Default Re: jython and concatenation of strings

Jan Gregor wrote:[color=blue]
> Hello
>
> I found that price of += operator on string is too high in jython. For
> example 5000 such operations took 90 seconds (i generated html copy of
> table with 1000 rows and 5 columns). Generation of row data into separate
> string and joining after lead to time 13 seconds !!!
>
> What's alternative way to do that ? (similiar parts of my code are terribbly
> slow and such simple solution as above didn't help).[/color]

I don't use Jython, but are you not able to do something like:

string_list = []
for ... in ...:
...
string_list.append(...)
...
string = ''.join(string_list)

This is the usual Python idiom and is usually faster than the += idiom.

Note too that +ing strings in Java also has this problem -- hence
StringBuffer or whatever it's called.

Steve
  #4  
Old July 18th, 2005, 07:45 PM
Jan Gregor
Guest
 
Posts: n/a
Default Re: jython and concatenation of strings

Ok, thanks. I didn't think that += operator is nondestructive operation
- but strings are immutable so this makes sense.

On 2004-12-13, Diez B. Roggisch <deetsNOSPAM@web.de> wrote:[color=blue][color=green]
>> I found that price of += operator on string is too high in jython. For
>> example 5000 such operations took 90 seconds (i generated html copy of
>> table with 1000 rows and 5 columns). Generation of row data into separate
>> string and joining after lead to time 13 seconds !!![/color]
>
> Its generally not recommended to use simple string concatenation for
> building larger strings - neither in python nor in java/jython.
>
> afaik there are two solutions to this: The java-way and the jython way:
>
> java: Use StringBuffer
> python: use a list, and append the strings to that. Then, when you want the
> result, do
>
> "".join(stringlist)
>[/color]
  #5  
Old July 18th, 2005, 07:47 PM
Jan Gregor
Guest
 
Posts: n/a
Default Re: jython and concatenation of strings

StringBuffer class from java was right solution - yours looses encoding,
and in jython I was unable to get it back - in python it worked fine.

Jan
[color=blue]
> I don't use Jython, but are you not able to do something like:
>
> string_list = []
> for ... in ...:
> ...
> string_list.append(...)
> ...
> string = ''.join(string_list)
>
> This is the usual Python idiom and is usually faster than the += idiom.
>
> Note too that +ing strings in Java also has this problem -- hence
> StringBuffer or whatever it's called.
>
> Steve[/color]
  #6  
Old July 18th, 2005, 07:47 PM
Nick Coghlan
Guest
 
Posts: n/a
Default Re: jython and concatenation of strings

Jan Gregor wrote:[color=blue]
> StringBuffer class from java was right solution - yours looses encoding,
> and in jython I was unable to get it back - in python it worked fine.[/color]

If you mean that Jython returned a string, when the inputs were unicode, then
that can probably be fixed with:

result = u''.join(string_list)

(Python switches to the unicode version automatically if it finds any unicode
strings in the supplied sequence. Jython may not do that - I'm not a Jython user
though, so I'm not sure).

Cheers,
Nick.

--
Nick Coghlan | ncoghlan@email.com | Brisbane, Australia
---------------------------------------------------------------
http://boredomandlaziness.skystorm.net
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 205,414 network members.