• Post Reply Bookmark Topic Watch Topic
  • New Topic

performance Issue

 
Michel Gordon
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Can nebody plz explain meWhy java.lang.String is a bad option as performance is concerned?

Thanks
michel
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Michel Gordon:
Can nebody plz explain meWhy java.lang.String is a bad option as performance is concerned?


No, because it's not true. Almost every Java program uses String.

If doing a lot of text manipulation, it is sometimes better, performance-wise, to use StringBuffer than String. A StringBuffer is mutable, whereas a String is not. When you do an operation that "changes" a String, what you actually get is a new String. This can involve unnecessary copying/allocation/deallocation, though String is a pretty smart class and won't copy the actual character data, until necessary.

However, worries about performance are very often ill-founded. Unless your application is processing huge amounts of text, then the differences between "fast" and "slow" text-processing code will be insignificant. Write your code in a clear and concise way, then see if it runs fast enough. If it doesn't, profile it. If the profile shows that the bottleneck is the text-processing, only then should you start optimising your text-processing.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with everything Peter wrote, but run this code, over lunch.
 
Ray Horn
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using StringBuilder, which removes thread synchronization, to compare with StringBuffer results in:

StringBuffer = 420
StringBuffer(UB) = 251
StringBuilder = 350
StringBuilder(UB) = 230

on old Pentium II box.
[ November 03, 2005: Message edited by: Ray Horn ]
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what was the time for String, itself?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By "String, itself" you mean String combined with the horrible, evil += concatenation operator? Don't blame String here, blame the concatenation - which is probably done with a StringBuilder here behind the scenes. But the problem is it's not able to reuse the StringBuilder; it creates a new one each time.
 
Ray Horn
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Didn't want to wait for 1 million iterations, but for 100,000 iterations on String:

String = 619000
 
jiju ka
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Albrechtsen, thanks for proving it using the code.

While I am writing this I have the foollowing output

StringBuffer = 140
StringBuffer(UB) = 16


It is more than 5 minutes now, the String time is not yet printed out.

By "String, itself" you mean String combined with the horrible, evil += concatenation operator?

Jim Yingst, the concatenation operator or gc is the culprit?. I try to avoid String as much as possible from computations. String being mutant, it adds to the heap size.

My understanding is String manipulation and gc don't go together. Here is the code(courtesy to Jeff Albrechtsen) to prove that this is true.



Value of UB is altered.
When UB is 500 GC is not performed. Results below..

StringBuffer = 32
StringBuffer(UB) = 0
String = 0


When UB is 1000 GC is not performed. Results below..

StringBuffer = 16
StringBuffer(UB) = 0
String = 15


When UB is 1000 * 10 GC is performed. Results below..

StringBuffer = 31
StringBuffer(UB) = 0
gc started
String = 516


When UB is 1000 * 20 GC is performed. Results below..

StringBuffer = 31
StringBuffer(UB) = 0
gc started
String = 2079


When UB is 1000 * 30 GC is performed. Results below..

StringBuffer = 32
StringBuffer(UB) = 0
gc started
String = 5062



Only two things need to be observed.
1. GC is not needed with String buffer because there is no mutable objects created.
2. With the amount of Strings created the GC time is increasing exponentially.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!