相关疑难解决方法(0)

clang vs gcc - 优化包括operator new

我有一个我正在测试的简单示例,我注意到当涉及operator new时,gcc优化(-O3)似乎不如clang那样好.我想知道可能是什么问题,是否有可能迫使gcc以某种方式生成更优化的代码?

template<typename T>
T* create() { return new T(); }

int main() {
    auto result = 0;
    for (auto i = 0; i < 1000000; ++i) {
        result += (create<int>() != nullptr);
    }

    return result;
}


#clang3.6++ -O3 -s --std=c++11 test.cpp
#size a.out
   text    data     bss     dec     hex filename
   1324     616       8    1948     79c a.out
#time ./a.out 
real 0m0.002s
user 0m0.001s
sys  0m0.000s

#gcc4.9 -O3 -s --std=c++11 test.cpp
#size a.out
   text    data     bss     dec     hex filename
   1484 …
Run Code Online (Sandbox Code Playgroud)

c++ gcc clang compiler-optimization c++11

7
推荐指数
3
解决办法
1409
查看次数

标签 统计

c++ ×1

c++11 ×1

clang ×1

compiler-optimization ×1

gcc ×1