Performances of Structs vs Classes

2020-02-03 05:42发布

问题:

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.

回答1:

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 A or class A in your code.

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 document you can read about other performance-related subtleties of C++.



回答2:

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



回答3:

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.



回答4:

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.



回答5:

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

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

my2c



回答6:

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.



回答7:

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.

see this article for details.