简体   繁体   English

减少非静态成员函数开销

[英]Reducing non-static member function overhead

I have a computational physics code (finite difference) that performs operations (derivatives) on large arrays of data (solution fields and coefficient fields).我有一个计算物理代码(有限差分),它对大型数据数组(解域和系数域)执行操作(导数)。 The coefficient data is stored as an array of Model objects with an overloaded () operator.系数数据存储为具有重载()运算符的Model对象数组。 The following class is an example of what I am trying to do:以下课程是我正在尝试做的一个例子:

class Model
{
public: 
    inline double operator () (double field)
    {
        return alpha*field;
    }
private:
double alpha ;
}

The application would use an array of Models :该应用程序将使用一组Models

std::vector<Model> models; // array of models
std::vector<double> input;
std::vector<double> output;
...    
for (int i = 0; i < models.size(); i++)
{
    output[i] = models[i](input[i]);
} 

The problem is that there is a significant overhead from using the () operator.问题是使用()运算符会产生很大的开销。

I've profiled the code as-is, and then again where I removed the () operator and wrote the code inline.我已经按原样分析了代码,然后再次删除了()运算符并内联编写了代码。 (I am using g++ 7.3.0., running on Ubuntu 18.04). (我使用的是 g++ 7.3.0。,在 Ubuntu 18.04 上运行)。 I am compiling with -Winline and g++ does not complain that it cannot inline the function.我正在使用-Winline编译,并且 g++ 没有抱怨它无法内联该函数。

I have also tried using a static function in place of the () operator, and that did not have the same overhead.我还尝试使用static函数代替()运算符,并且没有相同的开销。 Unfortunately a static function cannot access member variables.不幸的是,静态函数无法访问成员变量。 Other posts indicate that this should not be a problem ( eg C++ Non-static member functions overhead ) but it certainly seems to be here.其他帖子表明这应该不是问题(例如C++ 非静态成员函数开销),但它似乎确实存在。

So my question is: where is the overhead coming from with this non-static member function?所以我的问题是:这个非静态成员函数的开销来自哪里?

The difference between static and non-static is the this pointer.静态和非静态的区别在于this指针。 The code is shared between all instances, but there is an implicit argument to the function to access the members of the class.该代码在所有实例之间共享,但该函数有一个隐式参数来访问类的成员。 So there is no memory overhead like the question you refer but there is a small cost in time due to passing of this , and the pointer dereference to access the member variable.因此,没有像您提到的问题那样的内存开销,但是由于传递this和访问成员变量的指针取消引用,时间成本很小。 You could check the generated assembly file to make sure it's inline.您可以检查生成的程序集文件以确保它是内联的。

Suggestions to speed up the execution:加速执行的建议:

  • Use compiler optimizations使用编译器优化
  • Use Parallelism: C++ Threads or OpenMP are easy options here使用并行性:C++ 线程或 OpenMP 是这里的简单选项
  • Change your design to use a vector of alphas and remove the class, then you could perhaps use SIMD更改您的设计以使用 alpha 向量并删除该类,然后您也许可以使用 SIMD

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM