📄 main.cpp
字号:
#include <iostream>
#include "Timer.h"
using namespace std;
double fact(int n);
double fact1 (int n);
int main (){
double x, y, z, prod = 1.0;
Timer T, S;
T.start();
for(int i=0;i<1000000;i++){
y = fact(25);
}T.stop();
x = T.seconds();
z = T.time();
cout<<"Recursion time "<<z<<" clocks or "<<x<<" seconds"<<endl;
T.reset();
S.start();
for(int i=1;i<=1000000;i++){
y = fact1(25);
}
S.stop();
x = S.seconds();
z = S.time();
cout<<"Iteration time: "<<z<<" clocks or "<<x<<" seconds"<<endl;
return 0;
}
/*************** Recursive function ********************/
double fact(int n){
double r;
if(n==0 || n==1)
r=1;
else
r = n*fact(n-1);
return r;
}
/*************** Iterative function *******************/
double fact1 (int n){
double prod = 1.0;
for(int i=1;i<=n;i++){
prod = i*prod;
}
return prod;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -