Why sqrt in global scope is much slower than std::sqrt in MinGW?

余生颓废 提交于 2019-12-04 03:12:14

This is the typical situation in which the -fdump-tree-* switch can give some insight into what is going on:

g++ -fdump-tree-optimized example1.cc

and you get the example1.cc.165t.optimized file. Somewhere inside:

<bb 3>:
_5 = (double) i_2;
_6 = sqrt (_5);
sum_7 = sum_1 + _6;
i_8 = i_2 + 1;

The compiler (gcc v4.8.3) is doing the math with doubles.

Replacing sqrt with std::sqrt what you get is:

<bb 3>:
_5 = std::sqrt<int> (i_2);
sum_6 = sum_1 + _5;
i_7 = i_2 + 1;

Now it uses a different sqrt overload for integers (i_2 is int and sum_6 is double).

As Mike Seymour says in a comment, GCC uses the new overload whether or not you specify C++11.

Anyway under Linux there isn't a sensible performance difference between the two implementations.

Under Windows (MinGW) this is different since sqrt(double) calls into msvcrt.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!