C++ C++;用函数替换代码的最佳方法

C++ C++;用函数替换代码的最佳方法,c++,function,c++11,for-loop,C++,Function,C++11,For Loop,如果我有这样一个函数: void Func(T a) { T b, c, d; for (uint i = 0; i < 10000; ++i) { b = Call1(a); c = Call2(b); d = Call3(c); } } T Call(T a) { T b, c; b = Call1(a); c = Call2(b); d = Call3(c); r

如果我有这样一个函数:

void Func(T a)
{
    T b, c, d;
    for (uint i = 0; i < 10000; ++i)
    {
        b = Call1(a);
        c = Call2(b);
        d = Call3(c);
    }
}
T Call(T a)
{
    T b, c;
    b = Call1(a);
    c = Call2(b);
    d = Call3(c);
    return d;
}
Call
是否每次在循环中调用时都必须重新初始化
b
c
?我是否应该在
调用函数中使用
静态tb,c

您询问:

调用是否每次在循环中被调用时都必须重新初始化b和c

答案是肯定的

你问:

我是否应该在调用函数中使用静态tb,c

答案是,很可能不是

如果您担心创建
T
实例的成本,可以使用以下方法稍微优化函数:

T Call(T a)
{
    return Call3(Call2(Call1(a)));
}
你问:

调用是否每次在循环中被调用时都必须重新初始化b和c

答案是肯定的

你问:

我是否应该在调用函数中使用静态tb,c

答案是,很可能不是

如果您担心创建
T
实例的成本,可以使用以下方法稍微优化函数:

T Call(T a)
{
    return Call3(Call2(Call1(a)));
}

我将向你展示一个更一般化的问题解决方案。赫伯·萨特(Herb Sutter)在《更出色的C++》一书中有一章介绍了如何模拟嵌套函数:

template <class T> class F {
private:
  //T retval; // if you need Func to return
  //here you can have members that simulate local variables in the original Func
  // T x, y, z;

  // the nested function(s)
  void Call(a) {
    T b, c;
    b = Call1(a);
    c = Call2(b);
    d = Call3(c);
    // ... = x;
    return d;
  }

public:
  int F(T a) { // original function Func, now a consturctor
    T d;
    //x = ...
    for (uint i = 0; i < 10000; ++i)
        d = Call(a);
    // if you need the original function to return:
    // retval = ...;
  }
  // if the original function needs to return:
  T operator()() const {
    return retval;
  }

};
模板类F{
私人:
//T retval;//如果需要Func返回
//在这里,您可以让成员在原始Func中模拟局部变量
//tx,y,z;
//嵌套函数
无效通知(a){
tb,c;
b=1(a);
c=调用2(b);
d=3(c);
//…=x;
返回d;
}
公众:
int F(ta){//原函数Func,现在是constructor
td;
//x=。。。
对于(uint i=0;i<10000;++i)
d=呼叫(a);
//如果需要原始函数返回:
//retval=。。。;
}
//如果原始函数需要返回:
T运算符()()常量{
返回返回;
}
};

我将向您展示一个更通用的问题解决方案。赫伯·萨特(Herb Sutter)在《更出色的C++》一书中有一章介绍了如何模拟嵌套函数:

template <class T> class F {
private:
  //T retval; // if you need Func to return
  //here you can have members that simulate local variables in the original Func
  // T x, y, z;

  // the nested function(s)
  void Call(a) {
    T b, c;
    b = Call1(a);
    c = Call2(b);
    d = Call3(c);
    // ... = x;
    return d;
  }

public:
  int F(T a) { // original function Func, now a consturctor
    T d;
    //x = ...
    for (uint i = 0; i < 10000; ++i)
        d = Call(a);
    // if you need the original function to return:
    // retval = ...;
  }
  // if the original function needs to return:
  T operator()() const {
    return retval;
  }

};
模板类F{
私人:
//T retval;//如果需要Func返回
//在这里,您可以让成员在原始Func中模拟局部变量
//tx,y,z;
//嵌套函数
无效通知(a){
tb,c;
b=1(a);
c=调用2(b);
d=3(c);
//…=x;
返回d;
}
公众:
int F(ta){//原函数Func,现在是constructor
td;
//x=。。。
对于(uint i=0;i<10000;++i)
d=呼叫(a);
//如果需要原始函数返回:
//retval=。。。;
}
//如果原始功能需要返回:
T运算符()()常量{
返回返回;
}
};

使用内联函数怎么样?呼叫(b、c、d);我原以为编译器会在必要时内联它。那么使用内联函数呢?呼叫(b、c、d);我原以为编译器会在必要时内联它。好吧,使用
静态
会有什么问题?
static
是否会变慢,因为它不会将数据存储在缓存中?还有,有没有比
返回Call3(Call2(Call1(a))更好的方法因为当for循环中的代码变得更复杂时,这将开始变得非常麻烦,例如,如果我想要
if else
语句。函数中的
静态
变量只有在需要维护从一个调用到下一个调用的状态时才有意义。就性能而言,我认为
静态
变量不会改变任何东西。@Jean-Luc
静态
在性能方面可能更好,但它确实会使您的
调用
更脆弱。例如,它使它不是线程安全的。@RSahu如果有构造函数,那么
static
可能是一种优化;一个更常见的例子是
long string\u to_long(string s){istringstream i(s);long m(0);i>>m;return m;}
。将
i
设置为静态(即使每次都必须
.clear()
)是一个巨大的速度提升@MattMcNabb,过去我一直被
stringstream
相关的性能问题所困扰。谢谢提醒。好的,使用
静态
会有什么问题?
static
是否会变慢,因为它不会将数据存储在缓存中?还有,有没有比
返回Call3(Call2(Call1(a))更好的方法因为当for循环中的代码变得更复杂时,这将开始变得非常麻烦,例如,如果我想要
if else
语句。函数中的
静态
变量只有在需要维护从一个调用到下一个调用的状态时才有意义。就性能而言,我认为
静态
变量不会改变任何东西。@Jean-Luc
静态
在性能方面可能更好,但它确实会使您的
调用
更脆弱。例如,它使它不是线程安全的。@RSahu如果有构造函数,那么
static
可能是一种优化;一个更常见的例子是
long string\u to_long(string s){istringstream i(s);long m(0);i>>m;return m;}
。将
i
设置为静态(即使每次都必须
.clear()
)是一个巨大的速度提升@MattMcNabb,过去我一直被
stringstream
相关的性能问题所困扰。谢谢你的提醒。