Reputation: 3935
Say I've got a very lightweight object:
public class Point {
public int x;
public int y;
public Point(int ax, int ay){
x = ax;
y = ay;
}
}
And I need to calculate distance very frequently - for example, during a scroll event on a mobile device that might fire several times per second.
If it keeps the code a little cleaner and more transparent to use new Point(a, b) each time, is the performance hit significant enough that I should consider caching a few references and update the member variables (as opposed to instantiation)?
Upvotes: 2
Views: 1869
Reputation: 2603
Personally I would not worry about instantiating these objects each time you need a point. While yes, it would be a tiny bit faster to cache a few, I agree that it would make the code less clean. Check out what this Java performance optimization book has to say about object creation costs: https://www.safaribooksonline.com/library/view/java-performance-tuning/0596000154/ch04.html
Basically, if an old Pentium II could create a million lightweight objects per second, then I don't think you have anything to worry about given today's MUCH faster processors/memory/etc.
Upvotes: 5
Reputation: 57650
Updating previous instance will not allocate new memory but creating new instance will. Time to time jvm garbage collector will free memory. You don't have control over that. So it's always better to update existing instance unless you are keeping the history.
However you can always use a setter function.
public class Point {
public int x;
public int y;
public Point(int ax, int ay){
this.setXY(ax, ay);
}
public void setXY(int ax, int ay){]
x = ax;
y = ay;
}
}
Upvotes: 0
Reputation: 1286
You may also wish to read the following resource which sums up the performance costs of allocation in modern JVMs as well as the impacts of using object pooling which has many similarities to the object reuse scheme that you are considering.
http://www.ibm.com/developerworks/java/library/j-jtp01274/index.html
Possible JVM optimizations such as those referred to by the following quote should be taken into consideration.
The JIT compiler can perform additional optimizations that can reduce the cost of object allocation to zero.
However, I am not familiar specifically with the JVM implementations used in today's mobile devices and they may very well not be on par with the implementations used on regular computers.
In either case, if at all possible, it is generally discouraged to perform premature optimizations unless there is obvious and needed performance gains associated with it.
All in all, unless you measure that the allocation costs of your Point objects is taking up too much time you should not sacrifice code quality to improve it further.
Upvotes: 2
Reputation: 5492
You may use some instance variables instead of constructing objects on every call. This can effect your performance because this way you have to create and initialize the objects every time.
So its better to have a method call that updates the instance variables.
Upvotes: -1