使用time()函数计算执行时间

fud*_*din 0 c++ time ctime

我获得了以下HomeWork任务,

编写一个程序,在你的计算机上测试nlogn,n2,n5,2n和n需要多长时间!n = 5,10,15,20的加法.

我写了一段代码但是我一直都在执行时间.有人可以帮我解决吗?谢谢

#include <iostream>
#include <cmath>
#include <ctime>
using namespace std;
int main()
{
 float n=20;
 time_t start, end, diff;
  start = time (NULL);
  cout<<(n*log(n))*(n*n)*(pow(n,5))*(pow(2,n))<<endl;
  end= time(NULL);
 diff = difftime (end,start);
 cout <<diff<<endl;
 return 0;
}
Run Code Online (Sandbox Code Playgroud)

use*_*313 5

优于time()的第二精度是使用毫秒精度.便携式的方式是例如

int main(){
clock_t start, end;
double msecs;

start = clock();
/* any stuff here ... */
end = clock();
msecs = ((double) (end - start)) * 1000 / CLOCKS_PER_SEC;
return 0;
}
Run Code Online (Sandbox Code Playgroud)