#include<iostream>
#include<time.h>
inline double power1(double x,double y)
{
return x*x+y*y;
}
double power2(double x,double y)
{
return x*x+y*y;
}
int main(){
clock_t time_start1,time_end1;
LONG64 cnt=0;
LONG64 cnt_max=900000000^2;
time_start1=clock();
for (cnt=0;cnt<cnt_max;cnt++)
{
power1(5.2,45.2);
}
time_end1=clock();
std::cout<<"inline function time:"<<double(time_end1-time_start1)/CLOCKS_PER_SEC<<"s"<<std::endl;
clock_t time_start2,time_end2;
time_start2=clock();
for (cnt=0;cnt<cnt_max;cnt++)
{
power2(5.2,45.2);
}
time_end2=clock();
std::cout<<"No inline function time:"<<double(time_end2-time_start2)/CLOCKS_PER_SEC<<"s"<<std::endl;
return 0;
}
程序运行结果看:Inline 处理并不能提高程序的时间执行效率。
问题出在哪里呢?