• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Assignment 1a

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've been asked to optimize my assignment - offending line System.out.print( args[ 0 ] + " " ); -
nitpicker's comment -
100 array dereferences and 100 string concatenations. Any room for
optimization?
Can anyone point me in the right direction?
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey Debbie,
I had the same problem. See what we are doing here is concatenating the space to the word 100 times,ie each time just before printing.Why not add the space to the word just before the loop starts so that inside the loop the new word(args[0]+" ") gets printed.No math inside the loop!
Thats what I did and I passed!!!
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi peeps,
I had the same problem too. I'd just like to add to what Liza said.
Dereferencing the array element args[0] within the loop a hundred times is a bit heavy. Rather, it should be dereferenced outside the loop just once ,less work for the JVM that way. (Someone tell tell me if I'm wrong ...)
[This message has been edited by Eviano Afiemo (edited March 28, 2001).]
 
Ranch Hand
Posts: 351
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think this is the most common mistake made on assignment 1a. Most of us probably wrote that assignment and didn't think twice about dereferencing. Heck I didn't even know how to spell dereference.
I think this example teaches us to think about optimization regardless of the simplicity of the task. In a real world environment a little sloppy code can really fowl things up.
my .02
Mike
 
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String are immutable. So when you concatenate a string ( args[0] + " " ); behind the scenes several objects are being created to perform the concatenation. In a small program like this it might not be noticable but in bigger scale programs this could really slow the system down.


From Suns StringBuffer API:
String buffers are used by the compiler to implement the binary string concatenation operator +. For example, the code:
x = "a" + 4 + "c"
is compiled to the equivalent of:
x = new StringBuffer().append("a").append(4).append("c")
.toString()


Hope this helps...
Joe

[This message has been edited by Joseph Russell (edited March 28, 2001).]
 
Debbie Christian
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys, with all your help I passed 1a.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic