linux 测函数运行时间,程序运行时间测试 - 使用libc 中 time 函数 实现秒级的运行时间检测...

c 标准库中,有time 函数,可以返回 1970年1月1日 开始到现在的秒数,我们可以调用两次的时间差来计算程序运行时间:

name

time -- get time of day

library

standard c library (libc, -lc)

synopsis

#include

time_t

time(time_t *tloc);

description

the time() function returns the value of time in seconds since 0 hours, 0 minutes, 0 seconds, january 1, 1970, coordinated univer-

sal time, without including leap seconds. if an error occurs, time() returns the value (time_t)-1.

the return value is also stored in *tloc, provided that tloc is non-null.

/**

* author: yaowen xu

* github: https://github.com/yaowenxu

* organization: 北航系统结构研究所

* date: 2019-08-18 13:03:53

* lastedittime: 2019-08-18 13:14:33

* description: 使用 c 语言库 time 函数 对程序运行计时 以秒为单位

*/

#include

#include

#include

int str2int(char* str){

char *p = str;

int sum = 0;

while (*p != '\0')

{

sum = sum*10 + (*p-'0');

p++;

}

return sum;

}

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

time_t start, stop; // time_t aka long

int def = 1000;

if (argc == 2)

{

def = str2int(argv[argc-1]);

}

start = time(null);

for (int i = 0; i < def ; i++)

{

float tmp = sqrt(i);

}

stop = time(null);

time_t total = stop - start; // 使用运行的时间 以秒为单位

printf("start: %ld s\n", start);

printf("stop: %ld s\n", stop);

printf("time: %ld s\n", total); // 总共使用的时钟

return 0;

}

保持更新,如果对您有帮助,请点击推荐!更多关于c语言相关的知识,请关注 cnblogs.com/xuyaowen

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值