• Post Reply Bookmark Topic Watch Topic
  • New Topic

need help with Assignment 1.1  RSS feed

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For my solution, I keep getting the response: "100 string concatenations and array dereferences. Any room for optimization?"
Without putting my solution here, did anyone else have this problem and if so, can you give me a hint how to correct this?
Any help is much appreciated.
 
Sheriff
Posts: 9087
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
example string concatenation:
aString + anotherString

example array dereference:
array[0]

 
Trailboss
Posts: 22741
Firefox Browser IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do as much work as you can outside of the loop.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I also got the same comments about array dereference.
Could someone tell me what the issue is if I'm doing this. Is it something to do with the speed???
 
paul wheaton
Trailboss
Posts: 22741
Firefox Browser IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have an array, such as "args" and you want to grab, say, the first element of the array with args[0] - that is an array dereference. This take a little more work than just referring to the string in question directly.
 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony and Rajesh

I've recive the same message:

"100 string concatenations, Any room for optimization?"

I solved already.If you make a hundred contatenation you're making too much unnecesary work

Optimization ? well try to make as fews concatenations as posible.

this is probably out of date, but I wanna participate and may help someone else. )
 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, Rajesh, as far as I know this is an issue of speed. If you set args[0] as a variable then the program doesn't need to go check 100 times to see what args[0] is. Instead, it already "knows" (from memory) what the variable is and you can just print it without the program having to check anything. This is much faster. For this little program it probably wouldn't matter much but for larger programs checking huge arrays in many places it might make a significant difference.
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also (if we really want to get nitpicky!) use the String's <CODE>concat()</CODE> method as opposed to the String + oporator. It's more efficient. Check the following code and pcode:
<CODE>
public class Test{
public static void main( String[] args ){
String s = "124";
s = s.concat(s);
}
}
</CODE>
Produces the following:
D:\Yandon\CATTLE~1\ASSIGN~1.1>javap -c Test
Compiled from Test.java
public class Test extends java.lang.Object {
public Test();
public static void main(java.lang.String[]);
}
Method Test()
0 aload_0
1 invokespecial #5 <Method java.lang.Object()>
4 return
Method void main(java.lang.String[])
0 ldc #1 <String "124">
2 astore_1
3 aload_1
4 aload_1
5 invokevirtual #6 <Method java.lang.String concat(java.lang.String)>
8 astore_1
9 return

While this:
<CODE>
public class Test{
public static void main( String[] args ){
String s = "124";
s = s + s;
}
}
</CODE>
...produces this:
D:\Yandon\CATTLE~1\ASSIGN~1.1>javap -c Test
Compiled from Test.java
public class Test extends java.lang.Object {
public Test();
public static void main(java.lang.String[]);
}
Method Test()
0 aload_0
1 invokespecial #6 <Method java.lang.Object()>
4 return
Method void main(java.lang.String[])
0 ldc #1 <String "124">
2 astore_1
3 new #5 <Class java.lang.StringBuffer>
6 dup
7 aload_1
8 invokestatic #10 <Method java.lang.String valueOf(java.lang.Object)>
11 invokespecial #7 <Method java.lang.StringBuffer(java.lang.String)>
14 aload_1
15 invokevirtual #8 <Method java.lang.StringBuffer append(java.lang.String)>
18 invokevirtual #9 <Method java.lang.String toString()>
21 astore_1
22 return
 
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see how concat() is better than a StringBuffer. I also would like to know why concat() is faster than just using a +. What are the differences?
 
paul wheaton
Trailboss
Posts: 22741
Firefox Browser IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The important thing here is to move the work out of the loop. Do the work once instead of doing it a hundred times.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!