aspose file tools*
The moose likes Cattle Drive and the fly likes Basic optimization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » This Site » Cattle Drive
Bookmark "Basic optimization" Watch "Basic optimization" New topic
Author

Basic optimization

Scott Rumrill
Greenhorn

Joined: May 05, 2004
Posts: 10
So, I submitted my Assignemnt 1a for the 3rd time this morning, but I have a question in regards to optimization.

The way I origninally engineereed the solution is to loop and do 100 string concatenations, then print the names once. But I got nitpicked and was told that doing 100 string contcatenations was not the optimized way to do it. Instead, I should investigate printing the names as we go along.

My question is this: How do I know which is more/less optimized?:
- doing 100 string concatenations, OR
- printing output 100 times

Scott
Carol Murphy
village idiot
Bartender

Joined: Mar 15, 2001
Posts: 1195
Scott, you ask my kind of questions! I think the answer has to do with print as you go being more desirable than concatenating a bunch of strings, but the exact reason why escapes me at the moment. Someone wiser than I can explain the gory details. I got the same kind of nit-pick on my first assignment, but I didn't even know what optimization was way back then......
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10
Every time you concat two strings, you create about 4 objects. Object creation is expensive.

Especially in these early assignments I usually prefer print as you go because of the simplicity. If you are going to concat a bunch of Strings and just print them to the console at the end, why not just print them instead of concatenating them.

There are times when you might prefer to use other means of getting the String printed, but for now we are concentrating on simplicity and readability, with a little tiny bit of optimization thrown in.
[ May 21, 2004: Message edited by: Marilyn de Queiroz ]

JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Scott Rumrill
Greenhorn

Joined: May 05, 2004
Posts: 10
I'm not sure I understand this statement. The sentences seem to contradict each other. Either that, or I'm just not getting it.

Especially in these early assignments I usually prefer print as you go because of the simplicity. If you are going to concat a bunch of Strings and just print them to the console at the end, it's simpler.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10
That was definitely a confusing statement. That's what I get for posting when I'm so tired. I've modified/edited the above to be clearer (I hope).
Scott Rumrill
Greenhorn

Joined: May 05, 2004
Posts: 10
Thanks Marilyn. That clears things up quite a bit. Who knew that contatenating 2 strings creates 4 objects? Not me! When do we lean that stuff?

Will we be learning more optimization techniques during the rest of the Cattle Drive?
juliane gross
Ranch Hand

Joined: May 30, 2002
Posts: 161
Hi Scott,
you learn it "by doing" (means coding) and by getting nitpicked.
And you will learn lot's of it during all assignments.

I use this optimization guideline for myself:
avoid double code wherever possible.
And sometimes it is not even visible at first sight that there is double code indeed! for example, watch out for code inside methods which are far apart in your class file;
and for methods which seem to do different things but if you think about it long enough you suddenly see that what they do differently is only 1-2 lines of code and the rest could be combined in a third method.
Have fun!
Pauline McNamara
Sheriff

Joined: Jan 19, 2001
Posts: 4012
    
    6
Originally posted by Scott Rumrill:

Will we be learning more optimization techniques during the rest of the Cattle Drive?


Hi Scott,

The Cattle Drive really emphasizes writing simple readable code over optimization. Along the way, there may be a hint or two about improving performance, but it's definitely not a priority.

If you think of it on a larger scale, like the overall development and maintenance process, then improving readability just might help performance where it costs the most: when the next programmer, maybe someone with less experience than you, has to maintain your code - if it's not readable, then
Joyce Lee
Ranch Hand

Joined: Jul 11, 2003
Posts: 1392
Scott wrote:
My question is this: How do I know which is more/less optimized?:
- doing 100 string concatenations, OR
- printing output 100 times

Hi, Scott. A warm welcome to Cattle Drive College.

Doing String concatenation in a loop will create many StringBuffer objects. According to the
StringBuffer API, x = "a" + 4 + "c" is complied to the equivalent of
x = new StringBuffer().append("a").append(4).append("c").toString(). You can take a look at the bytecode with this command "javap -c Hundred". Remember to compile the code first before running javap.

By printing output 100 times is definitely slower than printing 1 time. As Cattle Drive emphasizes on readability and not so much on performance, hence printing output 100 times is acceptable. But this may not be the case in "real projects".

If you want to concatenate the Strings in a loop and call the println once, use StringBuffer. However, based on the assignments I've submitted so far, Cattle Drive does not seem to encourage the students to use StringBuffer.

Marilyn wrote:
Every time you concat two strings, you create about 4 objects. Object creation is expensive.

Does that mean we should take into consideration of the object creations? Object creations is related to performance. Does that mean we should use String concatenation rather than StringString() since readability is more important?

Pauline wrote:
If you think of it on a larger scale, like the overall development and maintenance process, then improving readability just might help performance where it costs the most: when the next programmer, maybe someone with less experience than you, has to maintain your code - if it's not readable, then

I've done maintenance projects before. And I agree with you that readability is important. This can be done by enforcing the programmers to follow the coding convention. Having good documentation is very important too for maintenance. But writing a simpler code just to accommodate the less experienced programmers may be not valid. Instead, the less experienced programmers should upgrade himself/herself and take this good opportunity to learn from the experienced one.

This is just my two cents' worth.

Joyce
[ June 03, 2004: Message edited by: Joyce Lee ]
paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 20582
    ∞

Marilyn asked me to stop by.

Code readability is about ten times more important than code performace. At least in the Cattle Drive. And in most programming shops too.

One cool thing is that most of the time when you make things more readable, you get optimization as a free bonus.

In this case, which is faster may be debatable. But one thing is for certain, using String for 100 string concatenations (most people try 200 on their first try) is a bad habit to get into.

As for facilitating junior programmers: I always liked that thing that said that professional writing should be geared toward the fifth grade reading level. One could argue that people should grow up. But then, you would decrease the size of your audience. On the other hand, note that it is not geared for second grade readers. So a line was drawn.

As a contractor, I get the stinky jobs that nobody else wants to do. A year ago I was in San Diego and inherited such a beast. Nobody wanted to mess with it. In the end I eliminated 99% of the known bugs, reduced the code size by a factor of four and doubled the functionality. Of course, any of these people could read the code. It was just a mess and would take more time. All I did was clean it up. Make it do the same thing, but in a more readable way. My focus was on readability.

It's weird how if you are willing to sacrifice 10% readability once in a while, it adds up over time. It compounds and compounds.

Outside of the cattle drive, you can make your own choices. If you choose to sacrifice readability, that's okay. It makes more work for guys like me.


permaculture Wood Burning Stoves 2.0 - 4-DVD set
Joyce Lee
Ranch Hand

Joined: Jul 11, 2003
Posts: 1392
Paul, thanks for stopping and sharing your experiences with us.

In this case, which is faster may be debatable. But one thing is for certain, using String for 100 string concatenations (most people try 200 on their first try) is a bad habit to get into.

If I use StringBuffer in a loop, does that make the code less readable?

As a contractor, I get the stinky jobs that nobody else wants to do. A year ago I was in San Diego and inherited such a beast. Nobody wanted to mess with it. In the end I eliminated 99% of the known bugs, reduced the code size by a factor of four and doubled the functionality. Of course, any of these people could read the code. It was just a mess and would take more time. All I did was clean it up. Make it do the same thing, but in a more readable way. My focus was on readability.

Could you share with us some of the examples that make a code less readable? I used StringBuffer in some of the assignments, but not encouraged. But why? StringBuffer is so common.

Joyce
[ June 04, 2004: Message edited by: Joyce Lee ]
paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 20582
    ∞

Using StringBuffer in a loop is great if you need a string in the end.

In this case, you need this to be sent to the console. Your choices are:

A) send it to the console as you go

B) store it in a StringBuffer as you go and send it to the console after the loop

I don't know about you, but option A seems simpler.
Joyce Lee
Ranch Hand

Joined: Jul 11, 2003
Posts: 1392
Choice A is good enough for this assignment. It's simpler since it's two lines lesser than choice B. Furthermore, the performance gained in choice B is not significant.

On the other hand, though choice B has two additional lines, it doesn't forgo the readability and it's a small gain in performance. So IMHO, both choices are equally acceptable. But if it's 100000 loops, I will go for choice B.

Joyce
[ June 04, 2004: Message edited by: Joyce Lee ]
paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 20582
    ∞

Originally posted by Joyce Lee:

On the other hand, though choice B has two additional lines, it doesn't forgo the readability


Two lines here. Two lines there. They add up. They compound. Most applications developed would be ten times smaller if they took out all of the uncalled for optimizations, "design by resume" features, "what if it rains" support and "wouldn't it be cool if ..." stuff. Ten times. Ten times faster to develop. According to managers, that would make you a ten times better developer. The program is ten times easier to enhance when needed. It has ten times fewer bugs (The most reliable components of any system are those that are not there).

Extreme Programming states that given two ways to do something and both get the job done, the one with the least code is the one to use. While one could probably find exceptions to this rule, I think it's a pretty good rule.

In this case, my opinion differs from yours: I think A is simpler and more readable. Therefore, it is what must be used until there is a business need compelling a change. "Business need" being defined as somebody outside of development requesting a change.

I think Marilyn made a good call. As usual.
Joyce Lee
Ranch Hand

Joined: Jul 11, 2003
Posts: 1392
Actually, while working on the assignments, it made me realise something, i.e., whether a code is readable, it's a very subjective issue. Sometimes, one may think adding a variable will make the code more readable, other may think it's not necessary. I got nitpicked by either introducing a variable or not.

Also, I don't really favour the approach used in Say 4b. Why must the students stick to this approach - loop unrolling? I understand one of the reasons is to make it easier for the instructors to grade. But some students may think that their approaches are not good enough.

Anyway, it's ok with me. Different people have different opinions and I respect your opinions.

Joyce
[ June 05, 2004: Message edited by: Joyce Lee ]
paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 20582
    ∞

One of the best ways to measure if something is readable is to have someone else read it. Hence, the nitpickers.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Basic optimization