Optimization: access to fields against methods

I know optimization rule # 1: don't do this! But I thought it was a simple question, and if I start using the faster method, now I can save a lot of CPU time when I finish.

I am doing an RPG and saying this is part of a custom class:

public class Baddie{

    int health;
    int magic;

    public Baddie(int health, int magic){
        this.health = health;
        this.magic = magic;
    }

    public int getHealth(){
        return health;
    }

Now the answer to my question may be "there is no difference", and it is wonderful with me .. I just want to know. Get Buddy's health faster by accessing the fields:

//Somewhere in the main thread, I get an instance of Baddie..
Baddie b = getScaryBadGuy();
int baddieHealth = b.health;

Or is it faster to use the return method?

int baddieHealth = b.getHealth();
+3
source share
4 answers

Copied and pasted from Design for Performance :

Getters/Setters

, ++, (, = getCount()) (i = mCount). ++, , .

Android . , , . - , , .

JIT 3 , . JIT ( , ), 7 , . true Froyo, , JIT .

+5

. . - , , , , , . , . , , , , .

+1

, . . , . " ". 2-3 , , , .

0

, . / - getter setter, . .

, , / , , .

0
source

Source: https://habr.com/ru/post/1772522/


All Articles