• Post Reply Bookmark Topic Watch Topic
  • New Topic

Code optimization  RSS feed

 
Jacob Sonia
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suppose there are two different pieces of code, how to find out which one is better if we are trying to focus on memory and time.
 
Victor Lu
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm just a beginner, but I would postulate the following:

1. The fewer variables you use the better.

2. If some variables will never change, mark them as constant (using the word "final") so that the computer knows it doesn't need to keep more space in case the variable changes.

3. After you finish using an object, set the object's reference variable to "null". Then the object will soon be removed from the computer's memory.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd disagree with the third point.

Don't set variables to null just because you think that somehow helps Java to clean objects up faster, without really knowing why or how this would work. Especially for local variables (declared inside methods) setting them to null explicitly does not do anything to help Java clean objects up.
 
Campbell Ritchie
Marshal
Posts: 56553
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree with Jesper. I read somewhere that setting references to null unnecessarily can be counter-productive.

... and welcome to the Ranch
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jacob Sonia wrote:Suppose there are two different pieces of code, how to find out which one is better if we are trying to focus on memory and time.
Who's time? the CPU? The User's? or the poor sap who has to look at the code in a year an try and figure out what the heck you were doing - and knowing full well that that sap may be YOU?

I cannot tell you the number of times I have written code, then gone back sometimes just a few weeks later and said "WHAT THE HECK WAS I DOING?" Then I spend hours or days trying to figure it out.

My point is that making micro-optimizations may, over the course of many months, save a few seconds of CPU time, maybe even a few minutes of users time...but that time is overshadowed by several orders of magnitude in lost developer time.

Your best bet is always, Always, ALWAYS, to write code that is clean, simple, and easy to understand. Only if there are specific, well defined, documented requirements for response time should you look at optimizing code.

I would therefore say the answer to your question is always "Whichever of the two is easier to read and understand".
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm with Fred. Therefore I also disagree with 1. If you write your code clean and with high cohesion and low coupling then in the case that you're having performance issues (because why make this faster when they don't need to be faster) it will be much easier to profile the application and make performance enhancements where they are necessary.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!