linux下的时间相关函数以及如何统计程序运行时间
2012-12-14 21:43
721 查看
Linux下时间有关的结构体:
1、timeval
timeval结构的用于指定时间值。
头文件: <sys/time.h>
2、timezone
tv_minuteswest是格林威治时间往西方的时差,tv_dsttime则是时间的修正方式。
3、timespec
tv_nsec是nano second(10E-9 second)。
4、tm
获取当前时间的方法:
1、time()
time_t time(time_t *t);
这个函数会传回从epoch开始计算起的秒数,如果t是non-null,它将会把时间值填入t中。
2、gettimeofday()
对某些需要较高精准度的需求,Linux提供了gettimeofday()
int gettimeofday(struct timeval * tv,struct timezone *tz);
int settimeofday(const struct timeval * tv,const struct timezone *tz);
3、struct tm格式时间函数
struct tm * gmtime(const time_t * t); 转换成格林威治时间。有时称为GMT或UTC。
struct tm * localtime(const time_t *t); 转换成本地时间。它可以透过修改TZ环境变数来在一台机器中,不同使用者表示不同时间。
time_t mktime(struct tm *tp); 转换tm成为time_t格式,使用本地时间。
tme_t timegm(strut tm *tp); 转换tm成为time_t格式,使用UTC时间。
double difftime(time_t t2,time_t t1); 计算秒差。
文字格式时间:
1、char * asctime(struct tm *tp); char * ctime(struct tm *tp);
这两个函数都转换时间格式为标准UNIX时间格式。
Mon May 3 08:23:35 1999
ctime一率使用当地时间,asctime则用tm结构内的timezone资讯来表示。
2、size_t
strftime(char *str,size_t max,char *fmt,struct tm *tp);
strftime有点像sprintf,其格式由fmt来指定。
下面举一个小例子,说明如何获得系统当前时间:
统计程序运行时间(us级别):
1、timeval
timeval结构的用于指定时间值。
头文件: <sys/time.h>
struct timeval { time_t tv_sec; //秒 [long int] suseconds_t tv_usec; //微秒 [long int] };
2、timezone
struct timezone { int tv_minuteswest; int tv_dsttime; };
tv_minuteswest是格林威治时间往西方的时差,tv_dsttime则是时间的修正方式。
3、timespec
struct timespec { long int tv_sec; long int tv_nsec; };
tv_nsec是nano second(10E-9 second)。
4、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()为负。*/ long int tm_gmtoff; /*指定了日期变更线东面时区中UTC东部时区正秒数或UTC西部时区的负秒数*/ const char *tm_zone; /*当前时区的名字(与环境变量TZ有关)*/ };
获取当前时间的方法:
1、time()
time_t time(time_t *t);
这个函数会传回从epoch开始计算起的秒数,如果t是non-null,它将会把时间值填入t中。
2、gettimeofday()
对某些需要较高精准度的需求,Linux提供了gettimeofday()
int gettimeofday(struct timeval * tv,struct timezone *tz);
int settimeofday(const struct timeval * tv,const struct timezone *tz);
3、struct tm格式时间函数
struct tm * gmtime(const time_t * t); 转换成格林威治时间。有时称为GMT或UTC。
struct tm * localtime(const time_t *t); 转换成本地时间。它可以透过修改TZ环境变数来在一台机器中,不同使用者表示不同时间。
time_t mktime(struct tm *tp); 转换tm成为time_t格式,使用本地时间。
tme_t timegm(strut tm *tp); 转换tm成为time_t格式,使用UTC时间。
double difftime(time_t t2,time_t t1); 计算秒差。
文字格式时间:
1、char * asctime(struct tm *tp); char * ctime(struct tm *tp);
这两个函数都转换时间格式为标准UNIX时间格式。
Mon May 3 08:23:35 1999
ctime一率使用当地时间,asctime则用tm结构内的timezone资讯来表示。
2、size_t
strftime(char *str,size_t max,char *fmt,struct tm *tp);
strftime有点像sprintf,其格式由fmt来指定。
下面举一个小例子,说明如何获得系统当前时间:
time_t now; struct tm *timenow; char strtemp[255]; time(&now); timenow = localtime(&now); printf("recent time is : %s \n", asctime(timenow))
统计程序运行时间(us级别):
struct timeval starttime, endtime; //get start time gettimeofday(&starttime, NULL); //operation ... //get end time gettimeofday(&endtime, NULL); //calculate consume us long cost_time = 1000000*(endtime.tv_sec - starttime.tv_sec) + endtime.tv_usec - starttime.tv_usec; cout << cost_time << endl;
相关文章推荐
- linux下c++多线程下用CLOCK()函数统计程序运行时间 2012-12-11 12:46:01 分类: C/C++ clock()函数的功能: 这个函数返回从“开启这个程序进程”到
- linux下统计程序/函数运行时间
- linux下统计程序/函数运行时间(转)
- Linux下静态库_库的基本概念;如何生成静态库动态库;nm查看库中包含那些函数、ar生成静态库,查看库中包含那些.o文件、ldd查看程序依赖的.so文件;gcc/g++与库相关的参数-L,-l,-f
- 如何记录 Linux 的系统运行时间的统计信息
- linux 统计 程序运行时间
- linux 统计 程序运行时间
- Linux下静态库_库的基本概念;如何生成静态库动态库;nm查看库中包含那些函数、ar生成静态库,查看库中包含那些.o文件、ldd查看程序依赖的.so文件;gcc/g++与库相关的参数-L,-l,-f
- Linux下如何查看系统启动时间和运行时间以及安装时间
- Linux下静态库_库的基本概念;如何生成静态库动态库;nm查看库中包含那些函数、ar生成静态库,查看库中包含那些.o文件、ldd查看程序依赖的.so文件;gcc/g++与库相关的参数-L,-l,-f
- Linux下静态库_库的基本概念;如何生成静态库动态库;nm查看库中包含那些函数、ar生成静态库,查看库中包含那些.o文件、ldd查看程序依赖的.so文件;gcc/g++与库相关的参数-L,-l,-f
- Unix/Linux环境C编程入门教程(22) C/C++如何获取程序的运行时间
- linux 统计 程序 运行时间
- linux 统计 程序运行时间
- Unix/Linux环境C编程入门教程(22) C/C++如何获取程序的运行时间
- Linux中统计程序运行所用时间
- c语言中如何添加计时函数,统计程序执行的时间
- linux下c++多线程下统计程序运行时间(非常注意)
- c语言中如何添加计时函数,统计程序执行的时间
- linux下c++多线程下统计程序运行时间(非常注意)