• 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

method calls vs local variable

 
Ranch Hand
Posts: 128
MS IE Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I.
String val=doJobAndGiveResult();
if(!val.equals("") && !val.equals("null") && val!=null){
//do something
}

II.
if(!doJobAndGiveResult().equals("") && !doJobAndGiveResult().equals("null") && doJobAndGiveResult()!=null){
//do something
}

Comparing I and II above, I think "I" will be more efficient and faster than "II" because "I" stores a reference(a local variable) and uses that reference. What do you think.

I'm trying to improve code for performance.
Thanks.
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Sylvester, using a reference to store the value should be faster specially if the method uses some time consuming operation to return the value.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If "doJobAndGiveResult()" returns a different result every time you call it, then the two formulations aren't equivalent. So beware of that when you are trying to optimize code.
 
Allen Bandela
Ranch Hand
Posts: 128
MS IE Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Paul,
If the method does not take parameters, and if the programmer isn't making sure in Case II. above that the doJobAndReturnResult() is returning the same value everytime it is called within the if condition, isn't the program faulty. How can the programmer rely on what value is returned each time. ( unless he has a counter in there and returns a value based on how many times its called).

Thanks.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"dojobandgiveresult" is pretty vague. we have no idea what it is doing. maybe it checks to see if some other conditions are met (is the vat full, have the lights been turned off, has the temp reached 127 degrees), and attempts to "do it's job" based on these conditions it has no control over. it could very easily return a different value each time it's called.
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
as a side note, trying to guess where a bottleneck is to improve code performance is probably not the best way to go. you may THINK this is where the slowdown is, but 99% of the time, you're wrong.

Use a profiler to figure out where the REAL slowdowns are. try and figure out how much time it'll take to optimize each spot, and figure out where is the best use of your time. sure, fixing this problem may speed things up, but will it make enough difference to be worth your time?
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you can still look at this situation and not take performance into account. You can look at this as good vs not so good coding practices. So let's make the assumption that oJobAndGiveResult() will return the same value.

Even though performance is not an issue per say, I still say that I is the way to do. Other opinions?
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sylvester James:
If the method does not take parameters, and if the programmer isn't making sure in Case II. above that the doJobAndReturnResult() is returning the same value everytime it is called within the if condition, isn't the program faulty.

Well, yes, it could well be faulty. But that's a separate concern from trying to speed it up. You should first get the program working correctly. Then, if you are trying to speed it up, you should not consider changes that make the program work differently. That was my main point -- in making the change from II to I you might make the program work differently.
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sylvester James:
If the method does not take parameters, and if the programmer isn't making sure in Case II. above that the doJobAndReturnResult() is returning the same value everytime it is called within the if condition, isn't the program faulty. How can the programmer rely on what value is returned each time. ( unless he has a counter in there and returns a value based on how many times its called).



There are a number of classes in the core API that are used every day which return a different *value* every time they are invoked without parameters. Iterator.next() comes to mind. Now you can argue that this is a faulty design, and there will be those that agree with you. Especially members of the functional programming community that program in languages that don't allow destructive updates. In those languages it is a way of life that a function invoked with the same parameters will *always* return the same result. Doing things that way allow optimizations such as once the return value is computed for a particular set of parameters, it can be memoized and never has to be computed again. Java being an imperative language that does support destructive updates makes no such guarantees. However this does provide the *advantage* that things like a random number generator, or I/O interaction are trivial in Java, not so in a pure functional language.

To be fair though, devotees of functional languages do believe that monads are a beautiful thing. I've not wrapped my mind around them to the point that I have the same amount of devotion though.

Hope this post wasn't too far off topic.
 
Allen Bandela
Ranch Hand
Posts: 128
MS IE Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I understand what everybody is saying and thank you for your insights.
I would like to once again refer to Case II for this reason:

Remember that multiple invocations of our method in question is being done in an if condition. Per say the method returns a value differently each time, I would state there is no functional goal reached by case II. It just runs alright.

Anyway, I have understood that I must understand how the method works to attempt any changes on Case II.

Thanks everybody.
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


The last test is either needless, or should be the first test, because if val is null, both leading calls will result in a NullPointerException, which can be avoided by resorting the tests, and if the method returns the same value each time it is called, the second approach should be changed accordingly.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic