inline 为什么没有效果?
我写了以下测试函数
inline int RoundFloatToInt(float f);
int RoundFloatToInt(float f)
{
int nResult;
__asm
{
fld f
fistp nResult
}
return nResult;
}
int main()
{
float f = 3.14188952f;
unsigned int t_start = timeGetTime();
for( int i=0; i<1000000; i++)
{
int k = (int)f;
}
unsigned int t_end = timeGetTime();
cout<<"force convert : "<< t_end - t_start<<endl;
t_start = timeGetTime();
for( int i=0; i<1000000; i++)
{
int k = RoundFloatToInt(f);
}
t_end = timeGetTime();
cout<< " asm convert: "<<t_end -t_start<<endl;
system("pause");
}
本来想看看优化后的float 2 int的有多少效率优势。
但当我看到force convert 14
asm convert 45 是,觉得结果不是我想要的。
再看汇编代码时发现做了很多函数的调用压栈等操作,所以速度就蛮下来了。
我再把inline关键字去掉 RoundFloatToInt函数的汇编代码一样。耗时在50左右。
那inline跟不inline不就一样了吗?
ps:我如果直接把RoundFloatToInt里的代码直接写到for循环里,耗时只有5;