Linux时间函数

linux的时间函数与windows上还是不完全一样的。

为了很好的处理时间,需要首先了解时间结构体定义,与常用函数的定义。

1.常用时间结构体定义:分为两类,以秒为级别的。以秒以下为级别的。

以秒为级别的:

time_t时间类型
time_t类型在time.h中定义:

可见,time_t实际是一个长整型。其值表示为从UTC(coordinated universal time)时间197011000000(也称为Linux系统的Epoch时间)到当前时刻的秒数。由于time_t类型长度的限制,它所表示的时间不能晚于2038119031407(UTC)。为了能够表示更久远的时间,可用64或更长的整形数来保存日历时间,这里不作详述。
使用time()函数获取当前时间的time_t值,使用ctime()函数将time_t转为当地时间字符串。

struct tm时间类型
tm结构在time.h中定义:

ANSI C标准称使用tm结构的这种时间表示为分解时间(broken-down time)
使用gmtime( )localtime( )可将time_t时间类型转换为tm结构体;

使用mktime( )tm结构体转换为time_t时间类型;
使用asctime( )struct tm转换为字符串形式。
struct timeval时间类型
timeval结构体在time.h中定义:

timespec结构体在time.h定义

 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都不是特别安全准确的,可以根据实际的运行条件选一种使用。

 

你可能感兴趣的:(Linux时间函数)