• Post Reply Bookmark Topic Watch Topic
  • New Topic

strings stuff  RSS feed

 
Asher Tarnopolski
Ranch Hand
Posts: 260
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
let's say i have a string, and i perform a number of operations on, f.e.:
myMethod1(my_string.substring(4,my_string.indexOf("lalala"));
myMethod2(my_string.substring(4,my_string.indexOf("lalala"));
myMethod3(my_string.substring(4,my_string.indexOf("lalala"));
the question is if it's not faster to allocate a new String object lie this:
String s=my_string.substring(4,my_string.indexOf("lalala");
and then reuse it:
myMethod1(s);
myMethod2(s);
myMethod3(s);
It's about the strings pool i guess..
thanX
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The second will be faster, absolutely. What you may not realize is that the first code is actually creating three new String objects, while the second code only creates one. The fact that the first code does not declare any new reference variables in unimportant - String objects are created, and references will be held in memory somewhere at least until they are passed to myMethod(). And anyway, references creation has minimal effect on performance, compared to object creation. So the second code is definitely faster.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is even more important: the first is also harder to maintain because of the duplicated logic!
Imagine that you need to change the way the substring gets calculated - you needed to change it at three different locations. If you need to do this in a hurry, you probably will end up with three different strings accidentally...
Additionally, by giving the local variable a better name than "s", you could also make it more obvious why you are building a substring.
So, although I would hesitate to do this because of performance optimization (do you already know the Three Rules Of Optimization?), I would definitely prefer the latter because of maintainability. See also http://www.refactoring.com/catalog/introduceExplainingVariable.html and http://www.refactoring.com/catalog/replaceTempWithQuery.html
[ June 10, 2002: Message edited by: Ilja Preuss ]
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is likely that the first is not creating new objects. It does go through the logic, but it will quickly find an existing String object in its pool and not need to make a new one. Nevertheless, the logic will be worked through each time so it will still be slower.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by CL Gilbert:
It is likely that the first is not creating new objects. It does go through the logic, but it will quickly find an existing String object in its pool and not need to make a new one. Nevertheless, the logic will be worked through each time so it will still be slower.

That's not true - the implementation of substring always creates a new object:
 
Michael Zalewski
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first block will indeed create three seperate String objects. What CL Gilbert was saying (I think) is that the first block won't necessarily allocate three seperate memory regions to hold the characters making up the Strings. He might be right. But you can't depend on it.
But my opinion is that the second block is more efficient, because only one String object is created.
In general, CL Gilbert is not correct. I tried the following using Java 1.2.2 006 from Sun:

It printed false, which shows that the strings were not canonicalized. That means the string data has been allocated to seperate areas of memory. Your actual mileage and results may vary, depending on platform and JDK version.
Of course, if I really wanted to test for String equality, I would use
sTest1.equals( sTest2)
or perhaps
if( sTest1.intern() == sTest2.intern()) ...
[ June 11, 2002: Message edited by: Michael Zalewski ]
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
interesting.
So I suppose the "lalala" is created only once, but the sub string is created everytime?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup. Although it's not an entirely new substring each time, as internally each String created by substring() uses the same char[] array as the parent String (but with different endpoints). So substring() is generally more efficient than, say, new String().
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!