C计算函数运行时间

关于时间的函数:

1:#include <time.h>

函数原型:     time_t time(time_t *timer)

C语言中通常是time(NULL)。

2:#include <time.h>

函数原型:     double difftime(time_t time2, time_t time1)

时间差 单位是秒。

3:#include <time.h>

函数原型:     struct tm *localtime(const time_t *timer)

获取当前时间和日期 已转换为当前时间

struct tm

{

int tm_sec; 代表目前秒数,正常范围为0-59,但允许至61秒

int tm_min; 代表目前分数,范围0-59

int tm_hour; 从午夜算起的时数,范围为0-23

int tm_mday; 目前月份的日数,范围01-31

int tm_mon; 代表目前月份,从一月算起,范围从0-11

int tm_year; 从1900 年算起至今的年数

int tm_wday; 一星期的日数,从星期一算起,范围为0-6

int tm_yday; 从今年1月1日算起至今的天数,范围为0-365

int tm_isdst; 日光节约时间的旗标

};

4:#include <sys/time.h>

函数原型:     int gettimeofday ( struct timeval * tv ,struct timezone * tz )

struct timeval{

  long tv_sec; //秒

  long tv_usec; //微妙

};

timezone 结构定义为:

   struct timezone{

   int tz_minuteswest; //时间差了多长时间

   int tz_dsttime;

};

5:#include <time.h>

函数原型:    clock_t clock(void)

精确到毫秒,经过1毫秒,函数的返回值+1

=================================================================

函数块:

1:

#include <time.h>

double diff_time;

clock_t start,end;

 

start = clock();

===============

函数运行

===============

end = clock();

diff_time = (double)((end-start)/CLK_TCK);

//diff_time = (double)((end-start)/CLOCKS_PER_SEC ) 获得进程自身的运行时间

printf("用时:%f ", diff_time)  :得到的差除1000(CLOCKS_PER_SEC)为秒 不除为毫秒。

2:

#include<time.h>

struct timeval t_start, t_end;

long diff_time;

gettimeofday(&t_start,NULL);

=====================

函数运行

=====================

gettimeofday(&t_end,NULL);

interval = 1000000*(end.tv_sec - start.tv_sec) + (end.tv_usec - start.tv_usec); //换成微妙

printf("interval = %f/n", interval/1000.0);  //换成ms毫秒

3:

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值