jython and concatenation of strings


J

Jan Gregor

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
 
Ad

Advertisements

D

Diez B. Roggisch

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 !!!

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

Steven Bethard

Jan said:
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).

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
 
J

Jan Gregor

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

Jan Gregor

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
 
Ad

Advertisements

N

Nick Coghlan

Jan said:
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.

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.
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top