码迷,mamicode.com
首页 > 编程语言 > 详细

C/C++ 時間相關函數

时间:2014-06-02 05:47:46      阅读:260      评论:0      收藏:0      [点我收藏+]

标签:c   style   a   tar   color   int   

最近在處理一些時間上面的問題,將這些函數整理一下囉

先從基本時間概念開始,比較要注意的應該是秒之下的時間單位

毫秒(ms):1秒=1000毫秒

之所以會看到這個時間單位是因為在影像處理裡面,如果是做到real-time的話

1秒要有30張frames,那就是一張frames要0.03333秒

 

C/C++中時間相關函數主要有兩個:clock()跟time()

這兩個函數都是在<time.h>裡面,使用的話要記得引用

 

clock()

函數定義

The C library function clock_t clock(void)returns the number of clock ticks elapsed since the program was launched. To get the number of seconds used by the CPU, you will need to divide by CLOCKS_PER_SEC.

On a 32bit system where CLOCKS_PER_SEC equals 1000000 this function will return the same value approximately every 72 minutes.

  簡單翻譯下重點:他是從程式開始時計算CPU運算的時間,需要用CLOCKS_PER_SEC去做分割

                   在32位元系統,CLOCKS_PER_SEC=1000000

回傳值

This function returns the number of clock ticks elapsed since the program start. On failure, the function returns a value of -1.

  它會回傳程序開始時間,最後會回傳毫秒

  以硬體滴答的時間為單位,以前TC2.0 下18.2個滴答為一秒,因此計算出來時間要除以18.2,在C中CLK_TCK 常被定義為18.2,但現在已經將其廢棄  

  而VC6.0之後CLK_TCK 取消使用了,一率使用CLOCKS_PER_SEC(VC6.0為1000)

  而MAC系列的定義就是1000000

  使用時要確認自己<time.h>的定義

  如果失敗則回傳-1

time()

函數定義

The C library function time_t time(time_t *seconds) returns the time since the Epoch (00:00:00 UTC, January 1, 1970), measured in seconds. Ifseconds is not NULL, the return value is also stored in variable seconds.

  從1970年1月1日開始的

回傳值

The current calendar time as a time_t object.

  需用time_t的變數去承接值

<time.h>中的定義

typedef long    clock_t; 
typedef unsigned        size_t;
typedef long    time_t;
typedef int     clockid_t;
typedef int     timer_t;
#define CLOCKS_PER_SEC          1000000
struct  tm {    /* see ctime(3) */
        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;
};

因為我這次要處理的是去紀錄IPCam大量的影片資訊(但我改用存大量的圖片作法)

第一個步驟就是圖片上面的名稱我要用時間來命名

第二個就是我的一秒有30張,但是這兩個函數看起來比較適合的是time(),但是精度只到秒

目前考慮做法是再加一個for迴圈,判斷時間是否為同一秒,同一秒的話,就再加計錄這一秒內的第幾張(理論上最多30張)

希望有做過這方面的高手能交流下

 

C/C++ 時間相關函數,布布扣,bubuko.com

C/C++ 時間相關函數

标签:c   style   a   tar   color   int   

原文地址:http://www.cnblogs.com/discipile/p/3763755.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!