2

考虑以下示例:

template<int X> class MyClass
{
    public:
        MyClass(int x) {_ncx = x;}
        void test() 
        {
            for (unsigned int i = 0; i < 1000000; ++i) {
                if ((X < 0) ? (_cx > 5) : (_ncx > 5)) {
                    /* SOMETHING */
                } else {
                    /* SOMETHING */
                }
            }
        }
    protected:
        static const int _cx = (X < 0) ? (-X) : (X);
        int _ncx;
};

我的问题是: MyClass<-6>::test() 和 MyClass<6>::test() 有不同的速度吗?

我希望如此,因为在模板参数为负的情况下,if可以在编译时评估 in test 函数,但我不确定如果存在编译时事物和非编译器,编译器的行为是什么- 三元运算符中的时间(这里就是这种情况)。

注意:这是一个纯粹的“理论”问题。如果有“是”的非空概率,我将为我的代码实现一些具有此类编译时模板参数的类,如果没有,我将只提供运行时版本。

4

3 回答 3

2

将条件移到循环外:

        ...
        if ((X < 0) ? (_cx > 5) : (_ncx > 5)) {
            for (unsigned int i = 0; i < 1000000; ++i) {
                /* SOMETHING */
            }
        } else {
            for (unsigned int i = 0; i < 1000000; ++i) {
                /* SOMETHING */
            }
        }
        ...

这样您就不必依赖编译器优化来删除未使用的代码;如果编译器没有删除条件的未使用部分,则您只需为条件分支支付一次费用,而不是每次循环。

于 2012-09-26T00:34:05.800 回答
2

对于我的编译器( OS X 上的 clang++ v2.9 )编译这个相似但不相同的代码:

void foo();
void bar();

template<int N>
void do_something( int arg ) {
  if ( N<0 && arg<0 ) { foo(); }
  else { bar(); }
}

// Some functions to instantiate the templates.
void one_fn(int arg) {
  do_something<1>(arg);
}

void neg_one_fn(int arg) {
  do_something<-1>(arg);
}

这将生成以下程序集clang++ -S -O3

one_fn = do_something<1>

第一个函数程序集显然只有对bar.

    .globl  __Z6one_fni
    .align  4, 0x90
__Z6one_fni:                            ## @_Z6one_fni
Leh_func_begin0:
    pushl   %ebp
    movl    %esp, %ebp
    popl    %ebp
    jmp __Z3barv                ## TAILCALL
Leh_func_end0:

neg_one_fn = do_something<-1>

第二个函数已简化为一个简单的 if 调用baror 或foo

    .globl  __Z10neg_one_fni
    .align  4, 0x90
__Z10neg_one_fni:                       ## @_Z10neg_one_fni
Leh_func_begin1:
    pushl   %ebp
    movl    %esp, %ebp
    cmpl    $0, 8(%ebp)
    jns LBB1_2                  ## %if.else.i
    popl    %ebp
    jmp __Z3foov                ## TAILCALL
LBB1_2:                                 ## %if.else.i
    popl    %ebp
    jmp __Z3barv                ## TAILCALL
Leh_func_end1:

概括

所以你可以看到编译器内联了模板,然后尽可能优化了分支。因此,您希望的那种转换确实发生在当前的编译器中。我也从旧的 g++ 4.0.1 编译器得到了类似的结果(但汇编不太清楚)。

附录:

我认为此示例与您的初始情况不太相似(因为它不涉及三元运算符)所以我将其更改为:(获得相同的结果)

template<int X>
void do_something_else( int _ncx ) {
  static const int _cx = (X<0) ? (-X) : (X);
  if ( (X < 0) ? (_cx > 5) : (_ncx > 5)) {
    foo();
  } else {
    bar();
  }
}

void a(int arg) {
  do_something_else<1>(arg);
}

void b(int arg) {
  do_something_else<-1>(arg);
}

这会生成程序集

a() = do_something_else<1>

这仍然包含分支。

__Z1ai:                                 ## @_Z1ai
Leh_func_begin2:
    pushl   %ebp
    movl    %esp, %ebp
    cmpl    $6, 8(%ebp)
    jl  LBB2_2                  ## %if.then.i
    popl    %ebp
    jmp __Z3foov                ## TAILCALL
LBB2_2:                                 ## %if.else.i
    popl    %ebp
    jmp __Z3barv                ## TAILCALL
Leh_func_end2:

b() = do_something_else<-1>

分支被优化掉了。

__Z1bi:                                 ## @_Z1bi
Leh_func_begin3:
    pushl   %ebp
    movl    %esp, %ebp
    popl    %ebp
    jmp __Z3barv                ## TAILCALL
Leh_func_end3:
于 2012-09-26T01:40:38.893 回答
0

这可能取决于您的编译器有多聪明。我建议您编写一个小基准程序,在您的环境中自己进行测试,以确保确定。

于 2012-09-25T23:37:29.103 回答