什么是比std :: pow更快? [英] What is faster than std::pow?
问题描述
我的程序在 std :: pow(double,int)
函数中花费了90%的CPU时间。准确性不是这里的主要关注点,所以我想知道是否有更快的选择。一个我想的尝试是转换为浮动,执行操作,然后回到双(还没有尝试过);我担心这不是一种提高性能的可移植方法(不是大多数CPU本质上都是双向操作的)
My program spends 90% of CPU time in the std::pow(double,int)
function. Accuracy is not a primary concern here, so I was wondering if there were any faster alternatives. One thing I was thinking of trying is casting to float, performing the operation and then back to double (haven't tried this yet); I am concerned that this is not a portable way of improving performance (don't most CPUs operate on doubles intrinsically anyway?)
干杯
推荐答案
It looks like Martin Ankerl has a few of articles on this, Optimized Approximative pow() in C / C++ is one and it has two fast versions, one is as follows:
inline double fastPow(double a, double b) {
union {
double d;
int x[2];
} u = { a };
u.x[1] = (int)(b * (u.x[1] - 1072632447) + 1072632447);
u.x[0] = 0;
return u.d;
}
这取决于在C ++中未定义行为的联合类型, 9.5 [class.union] :
which relies on type punning through a union which is undefined behavior in C++, from the draft standard section 9.5
[class.union]:
在并集中,最多一个非静态数据成员可以在任何时间处于活动状态,也就是说,
的值可以将大多数非静态数据成员存储在联合随时。 [...]
In a union, at most one of the non-static data members can be active at any time, that is, the value of at most one of the non-static data members can be stored in a union at any time. [...]
但大多数编译器包括 gcc支持此定义的行为:
从与最近写入的成员不同的成员(称为类型冲击)读取的做法是常见的。即使使用-fstrict别名,只要通过联合类型
The practice of reading from a different union member than the one most recently written to (called "type-punning") is common. Even with -fstrict-aliasing, type-punning is allowed, provided the memory is accessed through the union type
访问内存,则允许类型划分,但这不是通用的作为本文指出和我在我的回答在这里指出使用 memcpy
应该生成相同的代码,不会调用未定义的行为。
but this is not universal as this article points out and as I point out in my answer here using memcpy
should generate identical code and does not invoke undefined behavior.
他也链接到第二个为Java,C / C ++和C#优化pow()近似。
He also links to a second one Optimized pow() approximation for Java, C / C++, and C#.
第一篇文章还链接到他的microbenchmarks < a href =http://pastebin.com/DRvPJL2K =nofollow>此处
The first article also links to his microbenchmarks here
这篇关于什么是比std :: pow更快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!