c语言计算程序执行的时长,[C语言] 如何计算某一段程序执行时间

66b52468c121889b900d4956032f1009.png

8种机械键盘轴体对比

本人程序员,要买一个写代码的键盘,请问红轴和茶轴怎么选?

[C语言] 如何计算某一段程序执行时间

在Unix-like中,我们可以用 time [option] command 这种方法来计算出command执行所花费的时间,不过若是该command是一个daemon process, 我们一般来说 都只会监看log,此时若要知道某一段程序执行花费了多少时间, 当然我们可以用gettimeofday以及localtime_r来取得详细的日期与时间来进行相减的动作, 不过若是单纯只想计算时间,这边提供clock function来做更加的方便, 下方是使用的Sample Code

#include #include /* This sample will display how to calculate the program execution time */

int plus(int iEnd)

{

int i = 0;

while(i < iEnd)

i++;

return i;

}

int main(int argc, char *argv[])

{

clock_t start_time, end_time;

float total_time = 0;

int iNum = 0;

if (argc < 2)

{

printf("Usage : %s Numn", argv[0]);

return -1;

}

start_time = clock(); /* mircosecond */

iNum = plus(atoi(argv[1]));

end_time = clock();

/* CLOCKS_PER_SEC is defined at time.h */

total_time = (float)(end_time - start_time)/CLOCKS_PER_SEC;

printf("Time : %f sec n", total_time);

return 0;

}

===========================这是签名档分隔线==============================

我没有什么技术能力

不过却希望在这边跟大家分享自己遭遇的一些问题

希望大家有更好的方法可以跟我说!!======================================================================

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值