首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

c++程序运行时间解决办法

2012-07-31 
c++程序运行时间[codeC/C++][/code]#includecmath#includectime#includeiostream#includetime.hu

c++程序运行时间
[code=C/C++][/code]#include<cmath>
#include<ctime>
#include<iostream>
#include<time.h>
using namespace std;

int fib(int n){
 int fib=0;
 int i;
 int f1=1;
 int f2=1;
if(n==1 || n==2)
return 1;
else{
for(i = 3; i <= n; i++){
fib = f1 + f2;
f1 = f2;
f2 = fib;
}
  }
 
return fib;
}
int main(){
int N,result;
clock_t start,end;
cout << "Enter a Number: ";
cin >> N;
  start = clock();
  result = fib(N);
  int duration =(int) (end - start) / CLOCKS_PER_SEC ;
  cout << "The fib number is : " << result << endl;
cout << "The program takes "<< duration << endl;
  //system("Pause");
  return 0;
}
得出的运行时间为-1073,即使换N值也是一样的时间-1073,在linux环境下运行的,用的是putty.
请问怎么改?精确到毫秒的话是不是在CLOCLS_PER_SEC 后面*1000?谢谢

[解决办法]
使用gettimeofday
[解决办法]
你没计算end啊,大哥。

微妙级别gettimeofday。

热点排行