当前位置:首页 » 编程语言 » c语言计算时间函数吗
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

c语言计算时间函数吗

发布时间: 2022-12-08 18:14:28

c语言 时间函数

CLOCK()函数:
clock()是C/C++中的计时函数,而与其相关的数据类型是clock_t。在MSDN中,查得对clock函数定义如下:
clock_t
clock(void)
;
这个函数返回从“开启这个程序进程”到“程序中调用clock()函数”时之间的CPU时钟计时单元(clock
tick)数,在MSDN中称之为挂钟时间(wal-clock);若挂钟时间不可取,则返回-1。其中clock_t是用来保存时间的数据类型,在time.h文件中,我们可以找到对它的定义:
#ifndef
_CLOCK_T_DEFINED
typedef
long
clock_t;
#define
_CLOCK_T_DEFINED
#endif
很明显,clock_t是一个长整形数。在time.h文件中,还定义了一个常量CLOCKS_PER_SEC,它用来表示一秒钟会有多少个时钟计时单元,其定义如下:
#define
CLOCKS_PER_SEC
((clock_t)1000)
可以看到每过千分之一秒(1毫秒),调用clock()函数返回的值就加1。下面举个例子,你可以使用公式clock()/CLOCKS_PER_SEC来计算一个进程自身的运行时间:
void
elapsed_time()
{
printf("Elapsed
time:%u
secs.\n",clock()/CLOCKS_PER_SEC);
}
当然,你也可以用clock函数来计算你的机器运行一个循环或者处理其它事件到底花了多少时间:
#include
<stdio.h>
#include
<stdlib.h>
#include
<time.h>
int
main(void)
{
long
i
=
10000000L;
clock_t
start,
finish;
double
ration;
/*
测量一个事件持续的时间*/
printf(
"Time
to
do
%ld
empty
loops
is
",
i)
;
start
=
clock();
while(
i--
);
finish
=
clock();
ration
=
(double)(finish
-
start)
/
CLOCKS_PER_SEC;
printf(
"%f
seconds\n",
ration
);
system("pause");
}
在笔者的机器上,运行结果如下:
Time
to
do
10000000
empty
loops
is
0.03000
seconds
上面我们看到时钟计时单元的长度为1毫秒,那么计时的精度也为1毫秒,那么我们可不可以通过改变CLOCKS_PER_SEC的定义,通过把它定义的大一些,从而使计时精度更高呢?通过尝试,你会发现这样是不行的。在标准C/C++中,最小的计时单位是一毫秒。
time_t
time(
time_t
*timer
);
返回值是1970年到现在的秒数
用long型接就可以了
参数也是同样意义

long
time_s
=
0;
time_s
=
time(
NULL
);
//
time_s就是1970年到现在的秒数
或者
long
*
time_s
=
NULL;
time(time_s);
//
*time_s就是1970年到现在的秒数
要计算前后一段时间的话之前取一次time,之后取一次相减就知道用了多少秒了

❷ C语言中有没有一种计时函数,能算出从程序中某段代码运行所花的时间

C语言中有计时函数算时间,difftime,具体可以看一下<time.h>,这是标准C语言的头文件。
但注意这几件事,
1. 如果你是用单片机的嵌入式系统,time.h 中 涉及的几个函数很可能系统并没有实现,换句话说,嵌入式系统的时间处理可能不好用!因为嵌入式的系统一般来说不是标准系统。
2. 要统计算法的运行时间效率,单纯用计时的方法是很片面的。比如在windows下写两个程序,一个运行1秒,另一个运行2秒,并不能证明算法1的效率一定高于算法2。因为windows是典型的多任务系统,算法1可能花费了系统100%的资源处理了1秒,而算法2可能只花费了10%的资源处理了2秒。所以,计时的结果可以供参考,但不全面。通常评价算法的效率,还是要从算法本身来分析的。

❸ C语言时间函数,求解析意思

#include <stdio.h>
#include <time.h>
int main()
{
time_t timep; //时间变量,从1970年1月1日0时起的秒数
struct tm * p; //时间结构,含年月日时分秒星期几,一年中第几天,夏时制等成员。年从1900起算,月从0起算,...
time(&timep); // 获取当前时间,从1970年1月1日0时起的秒数
p = gmtime(&timep); // 获取UTC时间 结构成员数值们
printf("%d %d %d\n",1900+p->tm_year, 1+p->tm_mon, p->tm_mday); //输出UTC时间的年月日
p = localtime(&timep); // 获取本地 时间 结构成员数值们
printf("%d %d %d\n",1900+p->tm_year, 1+p->tm_mon, p->tm_mday); //输出本地时间年月日

return 0;
}

❹ c语言时间函数!!

time_t nowtime; -- 声明变量 nowtime(现在时间) 为 time_t 型
struct tm *timeinfo; -- 声明变量timeinfo(时间信息)为 tm 型 结构 指针。
time_t , tm 都是 time.h 头文件里定义 的 类型。
time( &nowtime ); -- 调系统函数 time(), 获得 现在时间 (1970年起多少个“滴答”,世界标准时间)
timeinfo = localtime( &nowtime ); -- 调系统函数, 获得 当地 现在时间 (例如 东8 区,北京时间)。时间数据是 tm 型 结构。
int hour; -- 声明变量 hour (小时),整型。
hour = timeinfo->tm_hour+1 ; -- 结构 timeinfo的成员tm_hour 是时间值,+1 得 hour(小时)。
tm_hour -- 数值范围 0-23。

❺ C语言时间函数time_t

1、time_t // 时间类型(time.h 定义)
struct tm { // 时间结构,time.h 定义如下:
int tm_sec;
int tm_min;
int tm_hour;
int tm_mday;
int tm_mon;
int tm_year;
int tm_wday;
int tm_yday;
int tm_isdst;
}
time ( &rawtime ); // 获取时间,以秒计,从1970年1月一日起算,存于rawtime
localtime ( &rawtime ); //转为当地时间,tm 时间结构
asctime() // 转为标准ASCII时间格式:
//就是直接打印tm,tm_year 从1900年计算,所以要加1900,月tm_mon,从0计算,所以要加1

2、time函数使用示例

#include<stdio.h>
#include<time.h>
intmain()
{
time_trawtime;
structtm*timeinfo;
time(&rawtime);
timeinfo=localtime(&rawtime);
printf("Thecurrentdate/timeis:%s",asctime(timeinfo));

return0;
}

❻ C语言的时间函数

程序似乎有问题,无效数据 输入什么都是无效的

❼ C语言计算时间函数

标准库的time.h里有时间函数

time_t time (time_t *timer)
计算从1970年1月1日到当前系统时间,并把结果返回给timer变量,
函数本身返回的也是这个结果.time_t这个类型其实就是一个int.

另有:
double difftime ( time_t timer2, time_t timer1 )
把返回time2和time1所储存的时间的差.

❽ 用c语言如何获取系统当前时间的函数

1、C语言中读取系统时间的函数为time(),其函数原型为:
#include <time.h>
time_t time( time_t * ) ;
time_t就是long,函数返回从1970年1月1日(MFC是1899年12月31日)0时0分0秒,到现在的的秒数。
2、C语言还提供了将秒数转换成相应的时间格式的函数:
char * ctime(const time_t *timer); //将日历时间转换成本地时间,返回转换后的字符串指针 可定义字符串或是字符指针来接收返回值
struct tm * gmtime(const time_t *timer); //将日历时间转化为世界标准时间(即格林尼治时间),返回结构体指针 可定义struct tm *变量来接收结果
struct tm * localtime(const time_t * timer); //将日历时间转化为本地时间,返回结构体指针 可定义struct tm *变量来接收结果
3、例程:
#include <time.h>
void main()
{
time_t t;
struct tm *pt ;
char *pc ;
time(&t);
pc=ctime(&t) ; printf("ctime:%s", pc );
pt=localtime(&t) ; printf("year=%d", pt->tm_year+1900 );
}

时间结构体struct tm 说明:

struct tm {
int tm_sec; /* 秒 – 取值区间为[0,59] */
int tm_min; /* 分 - 取值区间为[0,59] */
int tm_hour; /* 时 - 取值区间为[0,23] */
int tm_mday; /* 一个月中的日期 - 取值区间为[1,31] */
int tm_mon; /* 月份(从一月开始,0代表一月) - 取值区间为[0,11] */
int tm_year; /* 年份,其值等于实际年份减去1900 */
int tm_wday; /* 星期 – 取值区间为[0,6],其中0代表星期天,1代表星期一,以此类推 */
int tm_yday; /* 从每年的1月1日开始的天数 – 取值区间为[0,365],其中0代表1月1日,1代表1月2日,以此类推 */
int tm_isdst; /* 夏令时标识符,实行夏令时的时候,tm_isdst为正。不实行夏令时的进候,tm_isdst为0;不了解情况时,tm_isdst()为负。*/
};

❾ C语言函数的时间日期函数

函数库为time.h、dos.h
在时间日期函数里,主要用到的结构有以下几个:
总时间日期贮存结构tm
┌──────────────────────┐
│struct tm │
│{ │
│ int tm_sec; /*秒,0-59*/ │
│ int tm_min; /*分,0-59*/ │
│ int tm_hour; /*时,0-23*/ │
│ int tm_mday; /*天数,1-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; /*是否采用夏时制,采用为正数*/│
│} │
└──────────────────────┘
日期贮存结构date
┌───────────────┐
│struct date │
│{ │
│ int da_year; /*自1900的年数*/│
│ char da_day; /*天数*/ │
│ char da_mon; /*月数 1=Jan*/ │
│} │
└───────────────┘
时间贮存结构time
┌────────────────┐
│struct time │
│{ │
│ unsigned char ti_min; /*分钟*/│
│ unsigned char ti_hour; /*小时*/│
│ unsigned char ti_hund; │
│ unsigned char ti_sec; /*秒*/ │
│ │
└────────────────┘char *ctime(long *clock)
本函数把clock所指的时间(如由函数time返回的时间)转换成下列格式的
字符串:Mon Nov 21 11:31:54 1983
char *asctime(struct tm *tm)
本函数把指定的tm结构类的时间转换成下列格式的字符串:
Mon Nov 21 11:31:54 1983
double difftime(time_t time2,time_t time1)
计算结构time2和time1之间的时间差距(以秒为单位)
struct tm *gmtime(long *clock)本函数把clock所指的时间(如由函数time返回的时间)
转换成格林威治时间,并以tm结构形式返回
struct tm *localtime(long *clock)本函数把clock所指的时间(如函数time返回的时间)
转换成当地标准时间,并以tm结构形式返回
void tzset()本函数提供了对UNIX操作系统的兼容性
long dostounix(struct date *dateptr,struct time *timeptr)
本函数将dateptr所指的日期,timeptr所指的时间转换成UNIX格式,并返回
自格林威治时间1970年1月1日凌晨起到现在的秒数
void unixtodos(long utime,struct date *dateptr,struct time *timeptr)
本函数将自格林威治时间1970年1月1日凌晨起到现在的秒数utime转换成
DOS格式并保存于用户所指的结构dateptr和timeptr中
void getdate(struct date *dateblk)本函数将计算机内的日期写入结构dateblk
中以供用户使用
void setdate(struct date *dateblk)本函数将计算机内的日期改成
由结构dateblk所指定的日期
void gettime(struct time *timep)本函数将计算机内的时间写入结构timep中,
以供用户使用
void settime(struct time *timep)本函数将计算机内的时间改为
由结构timep所指的时间
long time(long *tloc)本函数给出自格林威治时间1970年1月1日凌晨至现在所经
过的秒数,并将该值存于tloc所指的单元中.
int stime(long *tp)本函数将tp所指的时间(例如由time所返回的时间)
写入计算机中.