Java – OOP design and smartphone performance

OOP design and smartphone performance… here is a solution to the problem.

OOP design and smartphone performance

I recommend this book: Getting Started with Android 4 Game Development by Mario Zechner and Robert Green, for beginners in Android games.

Page 192 of the book states:

The associated cost of method calls in Dalvik is higher than for other VMs. adopt
Use static methods if you can, as they perform best. Static methods are generally
Considered evil, much like static variables, try because they promote bad design
Make your design as clean as possible. Maybe you should avoid setter/getter and
The same goes for setters. Direct field access is approximately three times faster than methods
Without the call to JIT, using JIT is about seven times faster.
Still, consider your design before removing all getters and setters.

Now, does this have a big impact on the present? What’s really the best between performance and design? Because if I’m going to have static variables and methods, it’s in RAM until the app terminates, which would be bad if my app was too big and android 2.3 would be part of the market.

Solution

What developers often forget when doing micro-optimizations is that optimization should be done without breaking the design. Whether a field should be static or non-static is purely a design choice. Similarly, whether it makes sense to use getters and setters depends on whether you choose to set the field to private or public.

So, once you’ve completed your design phase and moved on to the implementation phase, then look at optimizations, such as using StringBuilder on String#concat. When you get to the implementation phase, you have already decided whether a field must be private, whether it has to be static, etc.

Now, to the technical partactually static method calls always ensure compile-time binding (bind). Therefore, it is cheaper to call them.

Usually direct field access is faster than using getter/setter

(JIT may be inline getter/setter, but it’s still more expensive than direct access).

So, this book is right about performance. But you shouldn’t compromise on your design in order to improve performance.

Related Problems and Solutions