Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
801 views
in Technique[技术] by (71.8m points)

class - C++ Non-static member functions overhead

I would like to know that, if we have the following class:

class MyClass  
{
public:
  MyClass(...)  
  type nonstatic_func1(...);
  type nonstatic_func2(...);
  ...
  type nonstatic_func10(...);
private:
  type var1;
  type var2;
  ...
  type var10;
};

Will each instance of MyClass have its own set of ten functions (i.e. for each instance, will a "version" of each of the ten functions be created)? How much will having, say, 20 functions in a class definition impact performance, as opposed to having, say, 2 functions (non-static), especially regarding instantiation, but also in working with these instances? How much will the amount of variables affect the performance? (see the next paragraph, the vector part)

The reason I am asking is I am writing a program that is instantiating a lot of instances of a class (to illustrate, I have quite a large vector, i.e. vector<MyClass> vec, for example), and the program is running slower than I anticipated.

In a nutshell, I'd like to know how much overhead there is in instantiating, and working with, an instance of a class that has a lot of non-static functions/variables.

EDIT

One of the things I do with my large vector of class instances is sorting...this is the main thing I suspect is draining performance, since there's a lot of moving (and copying, explicitly and implicitly) elements (instances) around and between vectors. Obviously if the chunk of data that has to be moved and copied so much is quite large, it could drain performance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Will each instance of MyClass have its own set of ten functions

No.

How much will having, say, 20 functions in a class definition impact performance, as opposed to having, say, 2 functions (non-static), especially regarding instantiation, but also in working with these instances?

Therefore, no.

How much will the amount of variables affect the performance?

The major impact of having a lot of member variable is that each instance occupies a lot of memory space. The consequence of being big in size is it would spend a lot of time when it is copied. A less obvious time overhead would be in CPU caching.

But these overhead may not be the causes of your problem.

One of the things I do with my large vector of class instances is sorting...this is the main thing I suspect is draining performance

Don't suspect. Measure. To track down where the performance goes, find out where the bottleneck is.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...