linux c 语言获取时间,linux C语言获取时间

#include // for printf()

#include // for gettimeofday()

#include // for sleep()

int main()

{

struct timeval start, end;

gettimeofday( &start, NULL );

printf("start : %d.%d\n", start.tv_sec, start.tv_usec);

sleep(1);

gettimeofday( &end, NULL );

printf("end : %d.%d\n", end.tv_sec, end.tv_usec);

return 0;

}

#include

#include

#include

int main(void)

{

struct timespec time_start={0, 0},time_end={0, 0};

clock_gettime(CLOCK_REALTIME, &time_start);

printf("start time %llus,%llu ns\n", time_start.tv_sec, time_start.tv_nsec);

clock_gettime(CLOCK_REALTIME, &time_end);

printf("end time %llus,%llu ns\n", time_end.tv_sec, time_end.tv_nsec);

printf("duration:%llus %lluns\n", time_end.tv_sec-time_start.tv_sec, time_end.tv_nsec-time_start.tv_nsec);

return 0;

}

#include

#include

#include

int main(){

clock_t start, finish;

double duration;

start = clock();

//run code

finish = clock();

duration = (double)(finish - start) / CLOCKS_PER_SEC;

printf( "%f seconds\n", duration );

return 0;

}

#include

#include

#include

#include

#include

#include

#define WAIT for(i=0;i<298765432;i++);

#define MILLION 1000000

int

main ( int argc, char *argv[] )

{

int i;

long ttt;

clock_t s,e;

struct tms aaa;

s=clock();

WAIT;

e=clock();

printf("clock time : %.12f\n",(e-s)/(double)CLOCKS_PER_SEC);

long tps = sysconf(_SC_CLK_TCK);

s=times(&aaa);

WAIT;

e=times(&aaa);

printf("times time : %.12f\n",(e-s)/(double)tps);

struct timeval tvs,tve;

gettimeofday(&tvs,NULL);

WAIT;

gettimeofday(&tve,NULL);

double span = tve.tv_sec-tvs.tv_sec + (tve.tv_usec-tvs.tv_usec)/1000000.0;

printf("gettimeofday time: %.12f\n",span);

struct timespec tpstart;

struct timespec tpend;

clock_gettime(CLOCK_REALTIME, &tpstart);

WAIT;

clock_gettime(CLOCK_REALTIME, &tpend);

double timedif = (tpend.tv_sec-tpstart.tv_sec)+(tpend.tv_nsec-tpstart.tv_nsec)/1000000000.0;

printf("clock_gettime time: %.12f\n", timedif);

return EXIT_SUCCESS;

}

9cfc0d1bca0d7f42a68f4ad3b143c861.png

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值