项目中有段核心代码大量用到了log,用常数屏蔽后发现性能有巨大提升.故百度一下:http://www.flipcode.com/archives/Fast_log_Function.shtml 好,找到一个fast log的实现:
// 上面链接中的源码有些地方不全,下面已经补齐了
static inline float fast_log2 (float val)
{
int* const exp_ptr = reinterpret_cast<int*>(&val);
int x = *exp_ptr;
const int log_2 = ((x >> 23) & 255) - 128;
x &= ~(255 << 23);
x += 127 << 23;
*exp_ptr = x;
val = ((-1.0f/3) * val + 2) * val - 2.0f/3; // (1)
return (val + log_2);
}
static inline float fast_log (float val)
{
return (fast_log2(val) * 0.69314718f);
}
本想着这个东东可以用来做优化,但很遗憾,这个fast log耗费的时间大概是VC自带库的4倍! 看来编译器的优化确实是非常牛的 :-)
评论