File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
chaitanya karthikk
Ranch Hand
Posts: 806
Java MySQL Database Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 33674
316
Eclipse IDE Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
chaitanya karthikk
Ranch Hand
Posts: 806
Java MySQL Database Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 3747
62
Chrome Netbeans IDE Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
Java Mac Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you chaitanya
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic