473,396 Members | 1,707 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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
Jul 18 '05 #1
5 6879
> 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)

--
Regards,

Diez B. Roggisch
Jul 18 '05 #2
Jan Gregor wrote:
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
Jul 18 '05 #3
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 <de*********@web.de> wrote:
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)

Jul 18 '05 #4
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
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

Jul 18 '05 #5
Jan Gregor wrote:
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.

--
Nick Coghlan | nc******@email.com | Brisbane, Australia
---------------------------------------------------------------
http://boredomandlaziness.skystorm.net
Jul 18 '05 #6

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

Similar topics

6
by: Jonathan | last post by:
Why did the designers of PHP decide to use period (".") as the string concatenation operator rater than the obviously more logical plus ("+")? //JJ
5
by: Jonas Galvez | last post by:
Is it true that joining the string elements of a list is faster than concatenating them via the '+' operator? "".join() vs 'a'+'b'+'c' If so, can anyone explain why?
1
by: Fahd Khan | last post by:
Hi team! While troubleshooting a crash I had while using BitTorrent where the torrent's target file names didn't fall into the ascii range I was playing around in the interpreter and noticed this...
37
by: Kevin C | last post by:
Quick Question: StringBuilder is obviously more efficient dealing with string concatenations than the old '+=' method... however, in dealing with relatively large string concatenations (ie,...
23
by: Bonj | last post by:
what is the correct form of string concatenation in VB.NET, + or &? Both seem to work, but which is correct? I know it's + in C# and & in VB6, but which in VB.NET?
3
by: Mythran | last post by:
Out of curiosity, only, which is recommended for SHORT concatenation...or concatenating two or three strings that are relatively small in size? Dim a As String = "bah" Dim b As String = "bah2"...
33
by: genc_ymeri | last post by:
Hi over there, Propably this subject is discussed over and over several times. I did google it too but I was a little bit surprised what I read on internet when it comes 'when to use what'. Most...
34
by: Larry Hastings | last post by:
This is such a long posting that I've broken it out into sections. Note that while developing this patch I discovered a Subtle Bug in CPython, which I have discussed in its own section below. ...
1
by: Ant | last post by:
Hi All, I'm looking to provide online python documentation for several jython modules: does anyone know if there are tools to create documentation from docstrings that work in Jython? Jython...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...
0
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...

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.