简体   繁体   English

c++ 结合了具有最佳性能的 function 指针

[英]c++ combining function pointers with best possible performance

I want to have a type describing a function that would allow creating new functions of the same type by combining existing functions, something like this:我想要一个描述 function 的类型,它允许通过组合现有函数来创建相同类型的新函数,如下所示:

FuncType f;
FuncType g;
FuncType h = f(g);
FuncType e = f + g;

I tried the using function pointers and assigning lambdas to them as follows:我尝试使用 function 指针并将 lambdas 分配给它们,如下所示:

typedef double(*FunPtr)(double);

double Fun1(double a) { return 2 * a; }
double Fun2(double a) { return a * a; }

int main{

  auto Fun3 = [](double a){return -a;};

  FuncType F1 = Fun1;
  FuncType F2 = Fun2;
  FuncType F3 = Fun3;

  auto HFun = [](double a){return (*F1)(a);} // does not work, requires capturing F1
  auto HFun = [F1](double a){return (*F1)(a);} // works
  FunPtr H = HFun; //Does not work, I suppose because of capturing F1.

}

Replacing更换

typedef double(*FunPtr)(double); typedef double(*FunPtr)(double);

with

typedef std::function FunPtr; typedef std::function FunPtr;

solves the issue, but the function calls are going to happen inside very large nested loops, hence performance can be an issue and I have read here and there that using std::function comes with an overhead.解决了这个问题,但是 function 调用将在非常大的嵌套循环中发生,因此性能可能是一个问题,我在这里和那里读到使用 std::function 会带来开销。

1- Is there a way to make this possible in a way that has a better performance compared to std::function? 1-与std :: function相比,有没有办法以具有更好性能的方式实现这一目标?

2- Does using function pointers have a better performance in the first place? 2-首先使用 function 指针有更好的性能吗?

3- Which of normal functions or lambdas is a better choice to start with? 3- 哪个普通函数或 lambdas 是更好的选择? (F3 vs F1) (F3 对 F1)

Thanks a lot!非常感谢!

1- Is there a way to make this possible in a way that has a better performance compared to std::function? 1-与std :: function相比,有没有办法以具有更好性能的方式实现这一目标?

std::function is very flexible, but it is indeed slow. std::function非常灵活,但确实很慢。 In order to be so flexible it performs type erasure and that has a cost.为了如此灵活,它执行类型擦除,这是有代价的。 I would guess that probably all other options are faster than std::function (always benchmark).我猜想可能所有其他选项都比std::function (总是基准测试)更快。

2- Does using function pointers have a better performance in the first place? 2-首先使用 function 指针有更好的性能吗?

Function pointers are very direct and the overhead should be negligible, but they are not very flexible. Function 指针非常直接,开销应该可以忽略不计,但它们不是很灵活。

3- Which of normal functions or lambdas is a better choice to start with? 3- 哪个普通函数或 lambdas 是更好的选择? (F3 vs F1) (F3 对 F1)

Lambda functions are much easier to use than than function pointers and they are efficient. Lambda 函数比 function 指针更容易使用,并且它们很有效。 But each lambda function is is "own type", which causes difficulties when you want to create an array of them.但是每个 lambda function 都是“自己的类型”,这在您想要创建它们的数组时会造成困难。

Lambda functions can be converted to function pointers, but only if they do not capture , but you need to capture to do the f(g) you want. Lambda 函数可以转换为 function 指针,但前提是它们不捕获,但您需要捕获才能执行所需的f(g) Therefore, in your case they don't seem to be an option因此,在您的情况下,它们似乎不是一种选择

My suggestion is that you try to keep using function pointers and if you can't for some reason then change to using virtual classes.我的建议是您尝试继续使用 function 指针,如果由于某种原因不能,则改用虚拟类。


In the past I had a need to store an array of functions and I started using an array of std::function which proved to be slow.过去我需要存储函数数组,我开始使用std::function数组,这被证明很慢。 In the end I changed to an array of base class pointers where the base class had a pure virtual method with the actual function to be implemented in subclasses.最后,我更改为基本 class 指针数组,其中基本 class 具有纯虚拟方法,实际 function 将在子类中实现。 Benchmark showed that this solution was faster than using std::function .基准测试表明,此解决方案比使用std::function更快。 Even though virtual methods also have some overhead it was less than the overhead in std::function .尽管虚拟方法也有一些开销,但它比std::function中的开销要少。

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

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