使用time()函数计算执行时间 [英] Calculating time of execution with time() function

查看:556
本文介绍了使用time()函数计算执行时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我获得了以下家庭作业分配,

I was given the following HomeWork assignment,


写一个程序在您的计算机上测试
nlogn,n2,n5,2n和n!添加n = 5,10,15,20。

Write a program to test on your computer how long it takes to do nlogn, n2, n5, 2n, and n! additions for n=5, 10, 15, 20.

我写了一段代码,任何人都可以帮助我吗?感谢

I have written a piece of code but all the time I am getting the time of execution 0. Can anyone help me out with it? Thanks

#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;
}


推荐答案

,以便您可以克服时间的低分辨率并获得有意义的结果。请记住在报告结果时除以迭代次数。

Execute each calculation thousands of times, in a loop, so that you can overcome the low resolution of time and obtain meaningful results. Remember to divide by the number of iterations when reporting results.

这不是特别准确,但可能与此作业无关。

This is not particularly accurate but that probably does not matter for this assignment.

这篇关于使用time()函数计算执行时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆