运行一下这个程序即可看出来。num的值不要设置太大。
递归函数大概是一般函数调用的2.5倍,是不用函数的8倍。
#include <iostream>
#include <ctime>
int x=0;
using namespace std;
long long sum=0;
void dizeng(int i)
{
if(i==0)return;
else
{
x++;
dizeng(i-1);
dizeng(i-1);
}
}
void jia(int i)
{
i++;
}
int main()
{
int t=clock();
int s=0;
int num=30;
dizeng(num);
printf("the number of run:%d/n",x);
int t2=clock();
printf("time of digui:%lf/n",(double(t2-t))/1000);
t=clock();
for(int i=1;i<x;i++)
jia(i);
t2=clock();
printf("time of yibanhanshu:%lf/n",(double(t2-t))/1000);
t=clock();
int y=0;
while(y<x)
{
y++;}
t2=clock();
printf("time of zhijiesuan:%lf/n",(double(t2-t))/1000);
system("pause");
}