C++记录代码运行时间

#include<time.h>
int main(){
	clock_t begin_time, end_clock;
	begin_time = clock();
    end_clock = clock();
	cout << "Running time is: " << static_cast<double>(end_clock - begin_time) / CLOCKS_PER_SEC << "s" << endl;//输出运行时间为秒
	cout << "Running time is: " << static_cast<double>(end_clock - begin_time) / CLOCKS_PER_SEC*1000 << "ms" << endl;//输出运行时间为毫秒
}
#include <iostream>
#include <time.h>

using namespace std;

int main() {

    time_t tt;
    time( &tt );
    tt = tt + 8*3600;  // transform the time zone
    tm* t= gmtime( &tt );
    cout << tt << endl;

    printf("%d-%02d-%02d %02d:%02d:%02d\n",
           t->tm_year + 1900,
           t->tm_mon + 1,
           t->tm_mday,
           t->tm_hour,
           t->tm_min,
           t->tm_sec);

    return 0;
}

linux
#include <sys/time.h>
void f()
{
  //...
}
int main()
{
  struct timeval t1, t2;
  gettimeofday(&t1, NULL);
  f();
  gettimeofday(&t2, NULL);
  //那么函数f运行所花的时间为
  //deltaT = (t2.tv_sec-t1.tv_sec) * 1000000 + t2.tv_usec-t1.tv_usec 微秒
  return 0;
}
linux
#include <time.h>
void f()
{
  //...
}
int main()
{
  timespec t1, t2;
  clock_gettime(CLOCK_MONOTONIC, &t1);
  f();
  clock_gettime(CLOCK_MONOTONIC, &t2);
  //那么f所花时间为
  //deltaT = (t2.tv_sec - t1.tv_sec) * 10^9 + t2.tv_nsec - t1.tv_nsec 纳秒
  return 0;
}
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值