wood burning stoves*
The moose likes Performance and the fly likes Method call which always returns same value Vs initializing a variable with the return value Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Performance
Bookmark "Method call which always returns same value Vs initializing a variable with the return value" Watch "Method call which always returns same value Vs initializing a variable with the return value" New topic
Author

Method call which always returns same value Vs initializing a variable with the return value

chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 800

Hi all, I have a method call in my class which reads a value from a file and returns it.I am using this in the same class like thisWhat I am thinking is to declare a variable like thisand use it like thisWhich one is a better approach? I think that whenever handler.receive(...) is executed getTimeOut() method is called which calls the getTimeOut(...) method of pasAdapter which I think makes more calls and effects the performance. Whereas if I declare a variable and use it in my code I don't think it effects the performance.

Can anyone please comment on this. Thank you in advance.

Love all, trust a few, do wrong to none.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30076
    
149

How long does getTimeout() take? How many times does it run? Unless it is a long time, the performance is negligible and you should go with what is clearer to read. I think that is not using it as a variable.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 800

I don't know how much time it takes. It is called always. Because it is called always I wanted to make it a local variable. This was my main intention.
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3606
    
  60

chaitanya karthikk wrote:I don't know how much time it takes. [...]

Then don't optimize it.

You should only try to optimize code after positively identifying a performance problem, usually by using a profiler. Anything else is just wild guessing. Without a profiler, you've no way to identify the code that will benefit most from optimization. You might easily spend several hours shaving off a few millisecond of runtime.
Murad Imanbayli
Greenhorn

Joined: Jan 24, 2013
Posts: 6

Thank you chaitanya


Murad Imanbayli - SCJP. I am from Azerbaijan
Leader of Baku JUG , My Java and Oracle blog
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Method call which always returns same value Vs initializing a variable with the return value
 
Similar Threads
Atomic Variables vs Synchronization
How to add two variables in XSLT
Help with objectifying code (constructor method) for simple compound interest quesion
MulitLock
"cannot find symbol variable..."