C++ 结构与类的表现

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5432681/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 18:09:31  来源:igfitidea点击:

Performances of Structs vs Classes

c++performanceclassstruct

提问by systemsfault

I wonder if there are performance comparisons of classes and C style structs in C++ with g++ -O3 option. Is there any benchmark or comparison about this. I've always thought C++ classes as heavier and possibly slower as well than the structs (compile time isn't very important for me, run time is more crucial). I'm going to implement a B-tree, should I implement it with classes or with structs for the sake of performance.

我想知道是否有使用 g++ -O3 选项对 C++ 中的类和 C 风格结构进行性能比较。是否有任何基准或比较。我一直认为 C++ 类比结构更重,可能也更慢(编译时间对我来说不是很重要,运行时间更重要)。我将要实现一个 B 树,为了性能,我应该用类还是用结构来实现它。

回答by Alexander Poluektov

On runtime level there is no difference between structs and classes in C++ at all. So it doesn't make any performance difference whether you use struct Aor class Ain your code.

在运行时级别,C++中的结构和类之间根本没有区别。因此,无论您使用struct A还是class A在您的代码中,它都不会产生任何性能差异。

Other thing, is using some features -- like, constructors, destructors and virtual functions, -- could have some performance penalties (but if you use them you probably need them anyway). But you can with equal success use them both inside your class or struct.

另一件事是使用一些特性——比如构造函数、析构函数和虚函数——可能会有一些性能损失(但如果你使用它们,你可能仍然需要它们)。但是你可以同样成功地在你的类或结构中使用它们。

In this documentyou can read about other performance-related subtleties of C++.

本文档中,您可以阅读 C++ 的其他与性能相关的细微之处。

回答by Oswald

In C++, structis syntactic sugar for classes whose members are public by default.

在 C++ 中,struct是默认情况下成员为 public 的类的语法糖。

回答by Mark Loeser

My honest opinion...don't worry about performance until it actually shows itself to be a problem, then profile your code. Premature optimization is the root of all evil. But, as others have said, there is no difference between a struct and class in C++ at runtime.

我诚实的意见...不要担心性能,直到它确实表明自己是一个问题,然后分析您的代码。过早的优化是万恶之源。但是,正如其他人所说,在运行时 C++ 中的结构和类之间没有区别。

回答by gregg

Focus on creating an efficient data structure and efficient logic to manipulate the data structure. C++ classes are not inherently slower than C-style structs, so don't let that limit your design.

专注于创建高效的数据结构和高效的逻辑来操作数据结构。C++ 类本质上并不比 C 风格的结构慢,所以不要让它限制你的设计。

回答by neuro

AFAIK, from a performance point of view, they are equivalent in C++.

AFAIK,从性能的角度来看,它们在 C++ 中是等效的。

Their difference is synctatic sugar like struct members are public by default, for example.

例如,它们的区别在于结构成员之类的同步糖默认是公开的。

my2c

我的2c

回答by ndrewxie

Just do an experiment, people!

做个实验吧,伙计们!

Here is the code for the experiment I designed:

这是我设计的实验的代码:

#include <iostream>
#include <string>
#include <ctime>
using namespace std;
class foo {
  public:
    void foobar(int k) {
      for (k; k > 0; k--) {
        cout << k << endl;
      }
    }
    void initialize() {
      accessor = "asdfasdfasdfasdfasdfasdfasdfasdfasdfasdf";
    }
    string accessor;
};
struct bar {
  public:
    void foobar(int k) {
      for (k; k > 0; k--) {
        cout << k << endl;
      }
    }
    void initialize() {
      accessor = "asdfasdfasdfasdfasdfasdfasdfasdfasdfasdf";
    }
    string accessor;
};

int main() {
  clock_t timer1 = clock();
  for (int j = 0; j < 200; j++) {
    foo f;
    f.initialize();
    f.foobar(7);
    cout << f.accessor << endl;
  }
  clock_t classstuff = clock();
  clock_t timer2 = clock();
  for (int j = 0; j < 200; j++) {
    bar b;
    b.initialize();
    b.foobar(7);
    cout << b.accessor << endl;
  }
  clock_t structstuff = clock();
  cout << "struct took " << structstuff-timer2 << endl;
  cout << "class took " << classstuff-timer1 << endl;
  return 0;
}

On my computer, struct took 1286 clock ticks, and class took 1450 clock ticks. To answer your question, struct is slightly faster. However, that shouldn't matter, because computers are so fast these days.

在我的电脑上,struct 需要 1286 个时钟滴答,而 class 需要 1450 个时钟滴答。要回答您的问题, struct 稍快一些。但是,这应该无关紧要,因为如今计算机的速度如此之快。

回答by AmjadHD

well actually structs can be more efficient than classes both in time and memory (e.g arrays of structs vs arrays of objects),

实际上,结构在时间和内存方面都比类更有效(例如,结构数组与对象数组),

??There is a huge difference in efficiency in some cases. While the overhead of an object might not seem like very much, consider an array of objects and compare it to an array of structs. Assume the data structure contains 16 bytes of data, the array length is 1,000,000, and this is a 32-bit system. For an array of objects the total space usage is: 8 bytes array overhea (4 byte pointer size × ((8 bytes overhead + = 28 MB For an array of structs, the results are dramatically different: 8 bytes array overhea (16 bytes data × 1,00 = 16 MB With a 64-bit process, the object array takes over 40 MB while the struct array still requires only 16 MB.

??在某些情况下效率存在巨大差异。虽然对象的开销可能看起来并不多,但请考虑对象数组并将其与结构数组进行比较。假设数据结构包含16个字节的数据,数组长度为1,000,000,这是一个32位系统。对于对象数组,总空间使用量为:8 字节数组开销(4 字节指针大小 ×((8 字节开销 + = 28 MB 对于结构数组,结果显着不同:8 字节数组开销(16 字节数据) × 1,00 = 16 MB 对于 64 位进程,对象数组占用超过 40 MB,而结构数组仍然只需要 16 MB。

see this articlefor details.

有关详细信息,请参阅本文