/////////////显示当前时间
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main()
{
time_t t;
time(&t);
printf("%s",ctime(t));
}
输出:Tue Jul 14 10:05:44 2009
#include<time.h>
time_t time(time_t *t);
此函数会返回从公元1970年1月1日的UTC时间从0时0分0秒算起到现在所经过的秒数。如果t 并非空指针的话,此函数也会将返回值存到t指针所指的内存。
#include<time.h>
struct tm *localtime(const time_t *t);
localtime()将参数timep所指的time_t结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果由结构tm返回。
localtime将日历时间转换为本地时间,gmtime将日历时间转化为国际标准时间。
结构tm的定义为
struct tm
{
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;
};
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 日光节约时间的旗标
#include<time.h>
char *asctime(const struct tm *tm);
asctime()将参数timeptr所指的tm结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果以字符串形态返回。
#include <sys/time.h>
#include <unistd.h>
int gettimeofday ( struct timeval * tv , struct timezone * tz )
timeval结构定义为:
struct timeval{
long tv_sec; /*秒*/
long tv_usec; /*微秒*/
};
使用:gettimeofday(tv,NULL);
#include<time.h>
char *ctime(const time_t *timep);
相当于asctime(localtime(*t))
2 unix时间值
a 日历时间 自1970年1月1日00:00:00以来经过的妙数。日历时间包括时间和日期。
b 进程时间
当度量一个进程的执行时间时,unix系统使用三个进程时间值
时钟时间 墙上cpu时钟时间,是进程运行的时间总量,其值与系统中同时运行的进程数有关。
用户cpu时间 执行用户指令所用的时间量
系统cpu时间 该进程执行内核所经历的时间。
#include <sys/times.h>
clock_t times(struct tms *buf);
struct tms {
clock_t tms_utime; /* user time */
clock_t tms_stime; /* system time */
clock_t tms_cutime; /* user time of children */
clock_t tms_cstime; /* system time of children */
};
////////////////////第一种计算程序运行时间的方法
#include<stdio.h>
#include<stdlib.h>
#include<sys/times.h>
#include<time.h>
#include<unistd.h>
void doit(char *str,clock_t time)
{
long tps=sysconf(_SC_CLK_TCK);
printf("%s:%6.6f secs\n",str,(float)time/tps);
}
int main()
{
clock_t start,end;
struct tms t_start,t_end;
int i;
start=times(&t_start);
for(i=0;i<100000;i++)
printf("%d\n",i);
end=times(&t_end);
doit("elapsed",end-start);
doit("user CPU",t_end.tms_cutime);
doit("sys CPU",t_end.tms_cstime);
}
////////////////////第二种计算程序运行时间的方法
int i;
float timeuse;
struct timeval tpstart,tpend;
gettimeofday(&tpstart,NULL);
////////////////////////
///////////////////////
gettimeofday(&tpend,NULL);
timeuse=1000000*(tpend.tv_sec-tpstart.tv_sec)+tpend.tv_usec-tpstart.tv_usec;
timeuse/=1000000;
printf("%f\n",timeuse);
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main()
{
time_t t;
time(&t);
printf("%s",ctime(t));
}
输出:Tue Jul 14 10:05:44 2009
#include<time.h>
time_t time(time_t *t);
此函数会返回从公元1970年1月1日的UTC时间从0时0分0秒算起到现在所经过的秒数。如果t 并非空指针的话,此函数也会将返回值存到t指针所指的内存。
#include<time.h>
struct tm *localtime(const time_t *t);
localtime()将参数timep所指的time_t结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果由结构tm返回。
localtime将日历时间转换为本地时间,gmtime将日历时间转化为国际标准时间。
结构tm的定义为
struct tm
{
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;
};
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 日光节约时间的旗标
#include<time.h>
char *asctime(const struct tm *tm);
asctime()将参数timeptr所指的tm结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果以字符串形态返回。
#include <sys/time.h>
#include <unistd.h>
int gettimeofday ( struct timeval * tv , struct timezone * tz )
timeval结构定义为:
struct timeval{
long tv_sec; /*秒*/
long tv_usec; /*微秒*/
};
使用:gettimeofday(tv,NULL);
#include<time.h>
char *ctime(const time_t *timep);
相当于asctime(localtime(*t))
2 unix时间值
a 日历时间 自1970年1月1日00:00:00以来经过的妙数。日历时间包括时间和日期。
b 进程时间
当度量一个进程的执行时间时,unix系统使用三个进程时间值
时钟时间 墙上cpu时钟时间,是进程运行的时间总量,其值与系统中同时运行的进程数有关。
用户cpu时间 执行用户指令所用的时间量
系统cpu时间 该进程执行内核所经历的时间。
#include <sys/times.h>
clock_t times(struct tms *buf);
struct tms {
clock_t tms_utime; /* user time */
clock_t tms_stime; /* system time */
clock_t tms_cutime; /* user time of children */
clock_t tms_cstime; /* system time of children */
};
////////////////////第一种计算程序运行时间的方法
#include<stdio.h>
#include<stdlib.h>
#include<sys/times.h>
#include<time.h>
#include<unistd.h>
void doit(char *str,clock_t time)
{
long tps=sysconf(_SC_CLK_TCK);
printf("%s:%6.6f secs\n",str,(float)time/tps);
}
int main()
{
clock_t start,end;
struct tms t_start,t_end;
int i;
start=times(&t_start);
for(i=0;i<100000;i++)
printf("%d\n",i);
end=times(&t_end);
doit("elapsed",end-start);
doit("user CPU",t_end.tms_cutime);
doit("sys CPU",t_end.tms_cstime);
}
////////////////////第二种计算程序运行时间的方法
int i;
float timeuse;
struct timeval tpstart,tpend;
gettimeofday(&tpstart,NULL);
////////////////////////
///////////////////////
gettimeofday(&tpend,NULL);
timeuse=1000000*(tpend.tv_sec-tpstart.tv_sec)+tpend.tv_usec-tpstart.tv_usec;
timeuse/=1000000;
printf("%f\n",timeuse);
posted on
浙公网安备 33010602011771号