C ++非静态成员函数开销 [英] C++ Non-static member functions overhead

查看:122
本文介绍了C ++非静态成员函数开销的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道,如果我们有以下类:

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;
};

MyClass 的每个实例都有自己的一组十个函数(即对于每个实例,将创建十个函数中的每一个的版本)?例如,在类定义中有20个函数会影响性能,而不是具有2个函数(非静态),特别是关于实例化,但是在使用这些实例时有多少?变量的数量会对性能产生多大的影响? (见下一段,向量部分)

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)

我问的原因是我正在写一个程序是实例化一个类的很多实例(为了说明,我有一个很大的向量,例如 vector< MyClass> vec ),比我预想的。

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.

推荐答案


每个MyClass实例都有自己的十个函数集

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

否。


在类定义中有20个函数会影响性能,而不是具有2个函数(非静态),尤其是关于实例化,

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?


$ b

变量的数量会对性能产生多少影响?

How much will the amount of variables affect the performance?

变量是每个实例占用大量的内存空间。大的大小的后果是它将花费大量的时间,当它被复制。不太明显的时间开销是在CPU缓存。

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.

这篇关于C ++非静态成员函数开销的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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