优化:访问字段与方法

发布于 2024-09-30 00:22:35 字数 652 浏览 5 评论 0原文

我知道优化的第一条规则是:不要这样做!但我认为这是一个简单的问题,如果我现在开始使用更快的方法,当我完成时我可以节省大量的 cpu 时间。

我正在制作一个角色扮演游戏,假设这是自定义类的一部分:

public class Baddie{

    int health;
    int magic;

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

    public int getHealth(){
        return health;
    }

现在,我的问题的答案可能是“没有区别”,这对我来说很好......我只是想知道。使用现场访问来获取坏人的健康状况是否更快:

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

还是使用返回方法更快?

int baddieHealth = b.getHealth();

I know rule #1 of optimization is: don't do it! But I figured this was an easy question, and if I start using the faster method now I can save a lot of cpu time when I'm finished.

I'm making an RPG, and let's say 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's no difference" and that's fine with me.. I just want to know. Is it quicker to get the Baddie's health using field access:

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

Or is it quicker to use a return method?

int baddieHealth = b.getHealth();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

紧拥背影 2024-10-07 00:22:35

性能设计复制并粘贴:

<块引用>

避免内部 Getter/Setter

在 C++ 等本地语言中,它是
使用吸气剂的常见做法(例如我
= getCount()),而不是直接访问该字段 (i = mCount)。这是
对于 C++ 来说这是一个极好的习惯,因为
编译器通常可以内联
访问,如果您需要限制或
调试字段访问您可以添加
随时代码。

在 Android 上,这是一个坏主意。
虚拟方法调用是昂贵的,
比实例字段更重要
查找。遵循是合理的
常见的面向对象编程
实践并有 getter 和 setter
在公共接口中,但在
类你应该总是访问字段
直接。

没有 JIT,直接字段访问是
比调用 a 大约快 3 倍
琐碎的吸气剂。使用 JIT(其中
直接现场访问的成本与
访问本地),直接字段
访问速度大约比
调用一个简单的 getter。这是
在 Froyo 中是这样,但在
未来当 JIT 内联 getter 时
方法。

Copied and pasted from Designing for Performance:

Avoid Internal Getters/Setters

In native languages like C++ it's
common practice to use getters (e.g. i
= getCount()) instead of accessing the field directly (i = mCount). This is
an excellent habit for C++, because
the compiler can usually inline the
access, and if you need to restrict or
debug field access you can add the
code at any time.

On Android, this is a bad idea.
Virtual method calls are expensive,
much more so than instance field
lookups. It's reasonable to follow
common object-oriented programming
practices and have getters and setters
in the public interface, but within a
class you should always access fields
directly.

Without a JIT, direct field access is
about 3x faster than invoking a
trivial getter. With the JIT (where
direct field access is as cheap as
accessing a local), direct field
access is about 7x faster than
invoking a trivial getter. This is
true in Froyo, but will improve in the
future when the JIT inlines getter
methods.

七婞 2024-10-07 00:22:35

性能总是相对的。通常最好用百分比或因子来思考。如果某件事需要一微秒,也许这就是很多,也可能什么都不是。这取决于您每秒需要执行多少次。这就是过早优化不受欢迎的主要原因,它是在不知道是否存在问题的情况下完成的。

Performance is always relative. It's usually better to think in terms of percentages or factors. If something takes a microsecond, maybe that's a lot, and maybe it's nothing. It depends on how many times per second you need to do it. That's the main reason premature optimization is frowned upon, it is done without knowing if there is a problem.

硪扪都還晓 2024-10-07 00:22:35

如果可以的话,编译器会进行优化。这是过早优化的一个完美例子。使用代码中任何有意义的内容。不用担心“节省周期”。任何其他操作所需的数百万个周期都超过了这可能会节省或可能不会节省的 2-3 个周期。

The compiler will optimize if it can. This is a perfect example of premature optimization. use whatever makes sense in your code. Don't worry about "saving cycles". The 2-3 cycles this may or may not save is outweighed by the millions of cycles it takes for any other operation.

刘备忘录 2024-10-07 00:22:35

在我看来,这更多的是一个设计问题而不是优化问题。我建议不要编写/生成任何 getter 或 setter,直到您确实需要从类外部访问它们。这往往会保持尽可能低的耦合。

或者,默认情况下将这些 getter/setter 设为私有会产生相同的结果,但代码更多,但没有真正的好处。

IMO it's more a design question than optimization question. I would suggest not writing/generating any getter or setter until you actually need them to be accessed from outside of your class. This tends to keep coupling as low as possible.

Alternatively making those getters/setters private by default would have the same result but it's more code for no real benefit.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文