大家都说sqrt()这个函数很慢,但是我想知道它究竟有多慢!

jianshi051 2008-03-06 07:25:23
没有什么测试的工具,也不知道sqrt函数是怎么实现的,只知道它很慢~

知道它比平方和慢,但是有时候用它的话考验一步解决,用平方和的话则要测试一些数字。。。

所以这个时候我不知道是用sqrt()好还是测试一堆平方和好了~ 看看大家有什么使用上的经验和心得吧
...全文
626 14 打赏 收藏 举报
写回复
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
FantasyNES 2009-04-23
  • 打赏
  • 举报
回复
fsqrt
jianshi051 2008-03-07
  • 打赏
  • 举报
回复
谢谢楼上的各位大侠,以后我的准则就是能不用就尽量不用吧~
晨星 2008-03-07
  • 打赏
  • 举报
回复
楼上说得对。
如果你的程序逻辑不可避免地要用到开平方的话。
那标准库中的sqrt不至于比你自己写的更低效。
taodm 2008-03-07
  • 打赏
  • 举报
回复
经验就是,在你一定要实现sqrt的功能时,就不要讨论它的速度.
星羽 2008-03-07
  • 打赏
  • 举报
回复


// 速的求1/sqrt(x)
//
float InvSqrt(float x){
float xhalf=0.5f*x;
long i=*(long*)&x;
i=0x5f3759df - (i>>1);
x=*(float *)&i;
x=x*(1.5f-xhalf*x*x);
return x;
}


Beyond3D.com的Ryszard Sommefeldt一直在想到底是哪个家伙写了这些神奇的代码?2003
年Chris Lomont还写了一篇文章(PDF)对这些代码进行了分析。毫无疑问写出这些代码的
人绝对是天才。
是John Carmack?Michael Abrash?John Carmack在邮件回复中明确表示不是他,也不是
Michael,可能是Terje Matheson。于是侦探Ryszard又向Terje Mathisen寻求答案。
Terje说他写过类似的高效代码,但上面的不是。他猜测可能在MIT的旧文档中可以找到。
Ryszard的求证之路显然无止境。

laolaoliu2002 2008-03-07
  • 打赏
  • 举报
回复
在标准库里面sqrt()好象是用逼近计算出来的.
星羽 2008-03-07
  • 打赏
  • 举报
回复
楼上那个汇编的函数太不精确了,其实到可以找找卡马克写的那个
abupie 2008-03-07
  • 打赏
  • 举报
回复
对现在的CPU来说,真的没有必要扣这么点性能。除非你是嵌入式。
agaric 2008-03-06
  • 打赏
  • 举报
回复
理论的算法似乎是级数求和。
jianshi051 2008-03-06
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 baihacker 的回复:]
sar 算术右移
[/Quote]


大侠,能解释一下第一个嵌套汇编的那个函数时怎么做的嘛? 为什么先减一个大数,再除2,再加回来啊?
baihacker 2008-03-06
  • 打赏
  • 举报
回复
sar 算术右移
jianshi051 2008-03-06
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 baihacker 的回复:]
C/C++ code
#include <iostream.h>
#include <math.h>
#include <windows.h>
/*
*浮点数开平方3种算法的速度测试
*阿金 2006-5-4
*z资料来源: http://greatsorcerer.go2.icpcn.com/info/fastsqrt.html
*
*测试结论:
*这些所谓高效的函数经过测试发现,还是sqrt()的速度最快。或者sqrtf()
*开始以为是函数调用时间的开销,于是把他们弄成inline,结果发现还是sqrt()的速度更快,
*又以为是程序先后顺序可能造…
[/Quote]

请问第一个函数里面汇编指令“sar”是什么指令?

那这样大家还都是用sqrt的了?
baihacker 2008-03-06
  • 打赏
  • 举报
回复

#include <iostream.h>
#include <math.h>
#include <windows.h>
/*
*浮点数开平方3种算法的速度测试
*阿金 2006-5-4
*z资料来源: http://greatsorcerer.go2.icpcn.com/info/fastsqrt.html
*
*测试结论:
*这些所谓高效的函数经过测试发现,还是sqrt()的速度最快。或者sqrtf()
*开始以为是函数调用时间的开销,于是把他们弄成inline,结果发现还是sqrt()的速度更快,
*又以为是程序先后顺序可能造成CPU或内存资源的问题,于是交换各个函数的执行顺序还是sqrt()的速度最快,
*又以为编译器对其进行过优化,把一些重复的部分给省略掉了,于是每处都加上一个rand()函数,输入加入rand()的结果,发现还是sqrt()的速度快,
*这样看来,可能是CPU的硬件厂商已经将该算法设计到硬件中,还是用math.h中的sqrt()吧~~
*/
////////////////////////test2;
inline float Faster_Sqrtf(float f)
{
float result;
_asm
{
mov eax, f
sub eax, 0x3f800000
sar eax, 1
add eax, 0x3f800000
mov result, eax
}
return result;
}
//////////////////////////test3

inline float CarmSqrt(float x){
union{
int intPart;
float floatPart;
} convertor;
union{
int intPart;
float floatPart;
} convertor2;
convertor.floatPart = x;
convertor2.floatPart = x;
convertor.intPart = 0x1FBCF800 + (convertor.intPart >> 1);
convertor2.intPart = 0x5f3759df - (convertor2.intPart >> 1);
return 0.5f*(convertor.floatPart + (x * convertor2.floatPart));
}
/////////////////////////////

void main()
{
float input,output1,output2,output3;
cout<<"input a float: "<<endl;
cin>>input;

unsigned long espTime1,espTime2,espTime3;
int temp;

unsigned long startTime=0, endTime=0, i=0,j=0,k=0;

///////////////////////////test3
startTime = timeGetTime();
for(i=0; i<100; i++)
{
for(j=0; j<1000; j++)
{
for(k=0; k<1000; k++)
{
temp = rand()%65535;
output3 = CarmSqrt(temp*input);
}
}
}
endTime = timeGetTime();
espTime3 = endTime - startTime;
/////////////////////////////test2
startTime = timeGetTime();
for(i=0; i<100; i++)
{
for(j=0; j<1000; j++)
{
for(k=0; k<1000; k++)
{
temp = rand()%65535;
output2 = Faster_Sqrtf(temp*input);
}
}
}
endTime = timeGetTime();
espTime2 = endTime - startTime;
/////////////////////////////test1
startTime = timeGetTime();
for(i=0; i<100; i++)
{
for(j=0; j<1000; j++)
{
for(k=0; k<1000; k++)
{
temp = rand()%65535;
output1 = sqrtf(temp*input);
}
}
}
endTime = timeGetTime();
espTime1 = endTime - startTime;

///////////////////////

cout<<"sqrt:"<<input<<" = "<<output1<<" espTime1:"<<espTime1<<endl;
cout<<"Fsqrt:"<<input<<" = "<<output2<<" espTime2:"<<espTime2<<endl;
cout<<"Csqrt:"<<input<<" = "<<output3<<" espTime3:"<<espTime3<<endl;


}

p0303230 2008-03-06
  • 打赏
  • 举报
回复
哦哦
发帖
C++ 语言

6.3w+

社区成员

C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
帖子事件
创建了帖子
2008-03-06 07:25
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下