linux的时间函数与windows上还是不完全一样的。
为了很好的处理时间,需要首先了解时间结构体定义,与常用函数的定义。
1.常用时间结构体定义:分为两类,以秒为级别的。以秒以下为级别的。
以秒为级别的:
time_t时间类型
time_t类型在time.h中定义:
#ifndef __TIME_T #define __TIME_T typedef long time_t; #endif
可见,time_t实际是一个长整型。其值表示为从UTC(coordinated universal time)时间1970年1月1日00时00分00秒(也称为Linux系统的Epoch时间)到当前时刻的秒数。由于time_t类型长度的限制,它所表示的时间不能晚于2038年1月19日03时14分07秒(UTC)。为了能够表示更久远的时间,可用64位或更长的整形数来保存日历时间,这里不作详述。
使用time()函数获取当前时间的time_t值,使用ctime()函数将time_t转为当地时间字符串。
struct tm时间类型
tm结构在time.h中定义:
#ifndef _TM_DEFINED 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],0代表星期天,1代表星期1,以此类推*/ int tm_yday; /*从每年的1月1日开始的天数-取值区间为[0, 365],0代表1月1日*/ int tm_isdst; /*夏令时标识符,使用夏令时,tm_isdst为正,不使用夏令时,tm_isdst为0,不了解情况时,tm_isdst为负*/ }; #define _TM_DEFINED #endif
ANSI C标准称使用tm结构的这种时间表示为分解时间(broken-down time)。
使用gmtime( )和localtime( )可将time_t时间类型转换为tm结构体;
使用mktime( )将tm结构体转换为time_t时间类型;
使用asctime( )将struct tm转换为字符串形式。
struct timeval时间类型
timeval结构体在time.h中定义:
Struct tmieval{ time_t tv_sec; /*秒s*/ suseconds_t tv_usec; /*微秒us*/ };timespec结构体在time.h定义
struct timespec{ time_t tv_sec; /*秒s*/ long tv_nsec; /*纳秒ns*/ };
Linux下常用时间函数有:time( )、ctime( )、gmtime( )、localtime( )、mktime( )、asctime( )、difftime( )、gettimeofday( )、settimeofday( )
clock_gettime();
其中gettimeofday和settimeofday clock_gettime是操作秒一下级别的函数。其他则是操作秒级别的函数(这里就不在举例测试)
测试环境是32位ubunbu11.10
#include <stdio.h>
//#include <time.h>//注视掉否则编译不过
#include <linux/time.h>
#define PERFORMANCE_ESTIMATE 1
struct timespec diff(struct timespec start, struct timespec end)
{
struct timespec temp;
if ((end.tv_nsec-start.tv_nsec) < 0) {
temp.tv_sec = end.tv_sec-start.tv_sec-1;
temp.tv_nsec = 1000000000+end.tv_nsec-start.tv_nsec;
} else {
temp.tv_sec = end.tv_sec-start.tv_sec;
temp.tv_nsec = end.tv_nsec-start.tv_nsec;
}
return temp;
}
int main()
{
struct timespec time11 = {0, 0};
struct timespec time12 = {0, 0};
struct timespec time21 = {0, 0};
struct timespec time22 = {0, 0};
struct timespec time31 = {0, 0};
struct timespec time32 = {0, 0};
struct timespec time41 = {0, 0};
struct timespec time42 = {0, 0};
struct timeval begin,end,gr,trans,crop,mcp, mBayer2NVHeap1, mBayer2NVHeap2, beforecallbacks;
gettimeofday(&begin,NULL);
#ifdef PERFORMANCE_ESTIMATE
gettimeofday(&mcp,NULL);
//LOGD("---test--- memory copy elapse time: %lu ms",
// ((mcp.tv_sec * 1000) + (mcp.tv_usec/1000)) - ((gr.tv_sec * 1000) + (gr.tv_usec/1000)));
#endif
//LOGD("~~~~pmem2.111`: %d, stream_w=%d, stream_h=%d~~~~",111, stream_w, stream_h);
//mNuCameraV4L2->bayer2NV12(pmem.virtualAddress, pmem2.virtualAddress, stream_w, stream_h, mBayerMode);
clock_gettime(CLOCK_REALTIME, &time11);
clock_gettime(CLOCK_MONOTONIC, &time21);
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time31);
clock_gettime(CLOCK_THREAD_CPUTIME_ID, &time41);
// mNuCameraV4L2->bayer2NV12(mBayer2NVHeap, pmem2.virtualAddress, stream_w, stream_h);
sleep(2);
usleep(2);
clock_gettime(CLOCK_REALTIME, &time12);
clock_gettime(CLOCK_MONOTONIC, &time22);
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time32);
clock_gettime(CLOCK_THREAD_CPUTIME_ID, &time42);
printf("=====bayer2NV12 1 elapse time: %lu ms\n",
((time12.tv_sec * 1000) + (time12.tv_nsec/1000/1000)) - ((time11.tv_sec * 1000) + (time11.tv_nsec/1000/1000)));
printf("=====bayer2NV12 2 elapse time: %lu ms\n",
((time22.tv_sec * 1000) + (time22.tv_nsec/1000/1000)) - ((time21.tv_sec * 1000) + (time21.tv_nsec/1000/1000)));
printf("=====bayer2NV12 3 elapse time: %lu ms\n",
((time32.tv_sec * 1000) + (time32.tv_nsec/1000/1000)) - ((time31.tv_sec * 1000) + (time31.tv_nsec/1000/1000)));
printf("=====bayer2NV12 4 elapse time: %lu ms\n",
((time42.tv_sec * 1000) + (time42.tv_nsec/1000/1000)) - ((time41.tv_sec * 1000) + (time41.tv_nsec/1000/1000)));
//#ifdef PERFORMANCE_ESTIMATE
gettimeofday(&trans,NULL);
printf("---test--- bayer2NV12 elapse time: %lu ms\n",
((trans.tv_sec * 1000) + (trans.tv_usec/1000)) - ((mcp.tv_sec * 1000) + (mcp.tv_usec/1000)));
}
=====bayer2NV12 1 elapse time: 2001 ms
=====bayer2NV12 2 elapse time: 2002 ms
=====bayer2NV12 3 elapse time: 0 ms
=====bayer2NV12 4 elapse time: 0 ms
---test--- bayer2NV12 elapse time: 2001 ms
在arm上的测试代码和结果。
clock_gettime(CLOCK_REALTIME, &time11);
clock_gettime(CLOCK_MONOTONIC, &time21);
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time31);
clock_gettime(CLOCK_THREAD_CPUTIME_ID, &time41);
mNuCameraV4L2->bayer2NV12(mBayer2NVHeap, pmem2.virtualAddress, stream_w, stream_h);
clock_gettime(CLOCK_REALTIME, &time12);
clock_gettime(CLOCK_MONOTONIC, &time22);
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time32);
clock_gettime(CLOCK_THREAD_CPUTIME_ID, &time42);
LOGD("=====bayer2NV12 1 elapse time: %lu ms",
((time12.tv_sec * 1000) + (time12.tv_nsec/1000/1000)) - ((time11.tv_sec * 1000) + (time11.tv_nsec/1000/1000)));
LOGD("=====bayer2NV12 2 elapse time: %lu ms",
((time22.tv_sec * 1000) + (time22.tv_nsec/1000/1000)) - ((time21.tv_sec * 1000) + (time21.tv_nsec/1000/1000)));
LOGD("=====bayer2NV12 3 elapse time: %lu ms",
((time32.tv_sec * 1000) + (time32.tv_nsec/1000/1000)) - ((time31.tv_sec * 1000) + (time31.tv_nsec/1000/1000)));
LOGD("=====bayer2NV12 4 elapse time: %lu ms",
((time42.tv_sec * 1000) + (time42.tv_nsec/1000/1000)) - ((time41.tv_sec * 1000) + (time41.tv_nsec/1000/1000)));
#ifdef PERFORMANCE_ESTIMATE
gettimeofday(&trans,NULL);
LOGD("---test--- bayer2NV12 elapse time: %lu ms",
((trans.tv_sec * 1000) + (trans.tv_usec/1000)) - ((mcp.tv_sec * 1000) + (mcp.tv_usec/1000)));
#endif
clock_gettime(CLOCK_REALTIME, &time11);
clock_gettime(CLOCK_MONOTONIC, &time21);
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time31);
clock_gettime(CLOCK_THREAD_CPUTIME_ID, &time41);
HandControlProcess(mBayer2NVHeap, 960, 480, uinput_report_touch_event, uinput_report_key_event, notify);
clock_gettime(CLOCK_REALTIME, &time12);
clock_gettime(CLOCK_MONOTONIC, &time22);
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time32);
clock_gettime(CLOCK_THREAD_CPUTIME_ID, &time42);
LOGD("=====HandControlProcess 1 elapse time: %lu ms",
((time12.tv_sec * 1000) + (time12.tv_nsec/1000/1000)) - ((time11.tv_sec * 1000) + (time11.tv_nsec/1000/1000)));
LOGD("=====HandControlProcess 2 elapse time: %lu ms",
((time22.tv_sec * 1000) + (time22.tv_nsec/1000/1000)) - ((time21.tv_sec * 1000) + (time21.tv_nsec/1000/1000)));
LOGD("=====HandControlProcess 3 elapse time: %lu ms",
((time32.tv_sec * 1000) + (time32.tv_nsec/1000/1000)) - ((time31.tv_sec * 1000) + (time31.tv_nsec/1000/1000)));
LOGD("=====HandControlProcess 4 elapse time: %lu ms",
((time42.tv_sec * 1000) + (time42.tv_nsec/1000/1000)) - ((time41.tv_sec * 1000) + (time41.tv_nsec/1000/1000)));
D/NuCameraHardware( 1133): =====HandControlProcess 1 elapse time: 113 ms
D/NuCameraHardware( 1133): =====HandControlProcess 2 elapse time: 114 ms
D/NuCameraHardware( 1133): =====HandControlProcess 3 elapse time: 180 ms
D/NuCameraHardware( 1133): =====HandControlProcess 4 elapse time: 110 ms
D/NuCameraHardware( 1133): =====end CallHCPThread() elapse time: 148 ms
D/NuCameraHardware( 1133): ---NuCameraHardware::callHCP() elapse time: 0 ms
I/ ( 1133): ENTER SETAGC ******times=0
D/NuCameraHardware( 1133): =====bayer2NV12 1 elapse time: 27 ms
D/NuCameraHardware( 1133): =====bayer2NV12 2 elapse time: 26 ms
D/NuCameraHardware( 1133): =====bayer2NV12 3 elapse time: 50 ms
D/NuCameraHardware( 1133): =====bayer2NV12 4 elapse time: 20 ms
D/NuCameraHardware( 1133): ---test--- bayer2NV12 elapse time: 27 ms
D/NuCameraHardware( 1133): ---end--- one frame elapse time: 47 ms
D/NuCameraHardware( 1133): ---NuCameraHardware::callHCP() elapse time: 0 ms
D/NuCameraHardware( 1133): =====bayer2NV12 1 elapse time: 12 ms
D/NuCameraHardware( 1133): =====bayer2NV12 2 elapse time: 12 ms
D/NuCameraHardware( 1133): =====bayer2NV12 3 elapse time: 40 ms
D/NuCameraHardware( 1133): =====bayer2NV12 4 elapse time: 20 ms
D/NuCameraHardware( 1133): ---test--- bayer2NV12 elapse time: 13 ms
D/NuCameraHardware( 1133): ---end--- one frame elapse time: 21 ms
D/NuCameraHardware( 1133): ---NuCameraHardware::callHCP() elapse time: 0 ms
D/NuCameraHardware( 1133): =====bayer2NV12 1 elapse time: 12 ms
D/NuCameraHardware( 1133): =====bayer2NV12 2 elapse time: 12 ms
D/NuCameraHardware( 1133): =====bayer2NV12 3 elapse time: 40 ms
D/NuCameraHardware( 1133): =====bayer2NV12 4 elapse time: 20 ms
D/NuCameraHardware( 1133): ---test--- bayer2NV12 elapse time: 13 ms
说明gettimeodday和clock_gettime都不是特别安全准确的,可以根据实际的运行条件选一种使用。