getter和setter是否会影响C ++ / D / Java中的性能? [英] Do getters and setters impact performance in C++/D/Java?

查看:584
本文介绍了getter和setter是否会影响C ++ / D / Java中的性能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个相当老的主题:setters和getter是好还是邪恶?

This is a rather old topic: Are setters and getters good or evil?

我的问题是:在
中编译器C ++ / D / Java
内联getter和setter?

My question here is: do compilers in C++ / D / Java inline the getters and setter?

getters / setter与直接字段访问相比,影响性能(函数调用,堆栈帧)的程度。除了使用它们的所有其他原因,我想知道他们是否应该影响性能,除了是一个良好的OOP实践。

To which extent do the getters/setters impact performance (function call, stack frame) compared to a direct field access. Besides all the other reasons for using them, I would like to know whether they are supposed to affect the performance besides being a good OOP practice.

推荐答案

这取决于。没有通用的答案总是成立的。

It depends. There is no universal answer that is always going to be true.

在Java中,JIT编译器可能会迟早内联。据我所知,JVM JIT编译器只优化大量使用的代码,所以你可以看到函数调用开销,直到getter / setter被频繁调用。

In Java, the JIT compiler will probably inline it sooner or later. As far as I know, the JVM JIT compiler only optimizes heavily used code, so you could see the function call overhead initially, until the getter/setter has been called sufficiently often.

在C ++中,它几乎肯定是内联的(假设启用了优化)。但是,有一种情况可能不会是:

In C++, it will almost certainly be inlined (assuming optimizations are enabled). However, there is one case where it probably won't be:

// foo.h
class Foo {
private:
  int bar_;

public:
  int bar(); // getter
};

// foo.cpp
#include "foo.h"

int Foo::bar(){
  return bar_;
}

如果函数的定义对类的用户不可见将包括foo.h,但不会看到foo.cpp),那么编译器可能无法内联函数调用。

If the definition of the function is not visible to users of the class (which will include foo.h, but won't see foo.cpp), then the compiler may not be able to inline the function call.

MSVC应该能够内联如果链接时间代码生成作为优化启用。我不知道GCC如何处理这个问题。

MSVC should be able to inline it if link-time code generation is enabled as an optimization. I don't know how GCC handles the issue.

通过扩展,这也意味着如果getter在不同的.dll / .so中定义,

By extension, this also means that if the getter is defined in a different .dll/.so, the call can not be inlined.

在任何情况下,我不认为琐碎的get / setters必须是良好的OOP实践是使用它们的所有其他原因。很多人认为小的get / setters 1)是一个糟糕的设计的标志,和2)浪费打字。

In any case, I don't think trivial get/setters are necessarily "good OOP practice", or that there are "all the other reasons for using them". A lot of people consider trivial get/setters to 1) be a sign of bad design, and 2) be a waste of typing.

个人而言,这不是我得到的东西任何一种方式。对我来说,为了符合良好的OOP实践的要求,它必须有一些可量化的积极效果。平凡的get / setters有一些边缘的优势,一些只是微不足道的缺点。因此,我不认为他们是一个好或坏的做法。他们只是你可以做的,如果你真的想要的。

Personally, it's not something I get worked up about either way. To me, for something to qualify as "good OOP practice", it has to have some quantifiable positive effects. Trivial get/setters have some marginal advantages, and some just as insignificant disadvantages. As such, I don't think they're a good or bad practice. They're just something you can do if you really want to.

这篇关于getter和setter是否会影响C ++ / D / Java中的性能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆