有關struct timeval結構體 以及 gettimeofday()函式
轉載地址:http://blog.chinaunix.net/uid-20548989-id-2533161.html
一、struct timeval結構體
一、struct timeval結構體
struct timeval結構體在time.h中的定義為:
其中,tv_sec為Epoch到建立struct timeval時的秒數,tv_usec為微秒數,即秒後面的零頭。比如當前我寫博文時的tv_sec為1244770435,tv_usec為442388,即當前時間距Epoch時間1244770435秒,442388微秒。需要注意的是,因為迴圈過程,新建結構體變數等過程需消耗部分時間,我們作下面的運算時會得到如下結果:
前面為微秒數,後面為秒數,可以看出,在這個簡單運算中,只能精確到小數點後面一到兩位,或者可以看出,每進行一次迴圈,均需花費0.005秒的時間,用這個程式來作計時器顯然是不行的,除非精確計算產生的程式碼消耗時間。
- struct timeval
- {
- __time_t tv_sec;
/* Seconds.
*/
- __suseconds_t tv_usec; /* Microseconds.
*/
- };
- #include <sys/time.h>
- #include <stdio.h>
-
- int
- main(void)
- {
- int i;
- struct timeval tv;
- for(i
= 0; i
< 4; i++){
- gettimeofday(&tv,
NULL);
- printf("%d\t%d\n", tv.tv_usec, tv.tv_sec);
- sleep(1);
- }
- return 0;
- }
- 329612 1314851429
- 329782 1314851430
- 329911 1314851431
- 330036 1314851432
二、gettimeofday()函式
原型:
- /*
Get the current time of
day and timezone information,
- putting it into *TV
and *TZ.
If TZ is
NULL, *TZ
is not filled.
- Returns 0 on success,
-1 on errors.
- NOTE: This
form of timezone information is obsolete.
- Use the functions and variables declared
in <time.h> instead.
*/
- extern int gettimeofday
(struct timeval *__restrict __tv,
- __timezone_ptr_t __tz) __THROW __nonnull ((1));
gettimeofday()功能是得到當前時間和時區,分別寫到tv和tz中,如果tz為NULL則不向tz寫入。
相關文章
- C時間函式strftime、struct timespec 和 struct timeval函式Struct
- fstat函式及struct stat結構函式Struct
- 結構體struct結構體Struct
- linux下時間有關的函式和結構體Linux函式結構體
- struct timeval 計時問題Struct
- interrupt結構體和相關函式結構體函式
- 結構 STRUCTStruct
- Golang 學習——結構體 struct (一)Golang結構體Struct
- Golang 學習——結構體 struct (二)Golang結構體Struct
- go 結構體 (struct) 和方法 (method)Go結構體Struct
- 瞭解下C# 結構體(Struct)C#結構體Struct
- 結構體定義:struct與typedef struct 用法詳解和用法小結結構體Struct
- struct 結構體 -Go 學習記錄Struct結構體Go
- golang 學習之路之 struct 結構體GolangStruct結構體
- C++ struct結構體記憶體對齊C++Struct結構體記憶體
- C++語言之結構體、類、建構函式、拷貝建構函式C++結構體函式
- 上下文 Context 與結構體 StructContext結構體Struct
- pat—結構體排序(用map彌補struct缺陷)結構體排序Struct
- C++ 結構體struct和共同體union的區別C++結構體Struct
- 有關箭頭函式函式
- 【PB】有關日期函式函式
- 關於建構函式與解構函式的分享函式
- 結構體定義 typedef struct 用法詳解和用法小結結構體Struct
- C語言中結構體struct的對齊問題C語言結構體Struct
- C# 中的只讀結構體(readonly struct)C#結構體Struct
- Effective c++(筆記) 中關於建構函式、解構函式以及賦值操作符的知識C++筆記函式賦值
- 認知結構(C# Struct)C#Struct
- 和Null有關的函式Null函式
- Golang物件導向程式設計之建構函式【struct&new】Golang物件程式設計函式Struct
- Golang 註釋規範-類、函式、結構體等Golang函式結構體
- 原來結構體函式還能這樣用結構體函式
- struct結構體大小的計算(記憶體對齊)Struct結構體記憶體
- Solidity語言學習筆記————15、結構體StructSolid筆記結構體Struct
- 詳解js原型,建構函式以及class之間的原型關係JS原型函式
- 結構體相關結構體
- 有關日期的SAP函式使用函式
- match函式簡單介紹以及與index函式結合應用函式Index
- struct 和 interface:結構體與介面都實現了哪些功能?Struct結構體