1. 程式人生 > >c函式之gettimeofday()函式獲取系統時間

c函式之gettimeofday()函式獲取系統時間

gettimeofday

    使用C語言編寫程式需要獲得當前精確時間(1970年1月1日到現在的時間),或者為執行計時,可以使用gettimeofday()函式。

函式原型:

    int gettimeofday(struct timeval*tv, struct timezone *tz);

所需標頭檔案:

    #include <sys/time.h>

說明:

    其引數tv是儲存獲取時間結果的結構體,引數tz用於儲存時區結果:
struct timezone{
    int tz_minuteswest;/*格林威治時間往西方的時差*/
    int tz_dsttime;/*DST 時間的修正方式*/
}
    timezone 引數若不使用則傳入NULL即可。     而結構體timeval的定義為:
struct timeval{
    long int tv_sec; // 秒數
    long int tv_usec; // 微秒數
}

    它獲得的時間精確到微秒(1e-6 s)量級。在一段程式碼前後分別使用gettimeofday可以計算程式碼執行時間:
struct timeval tv_begin, tv_end;
gettimeofday(&tv_begin, NULL);
foo();
gettimeofday(&tv_end, NULL);

例項:

#include <sys/time.h>
#include <stdio.h>
#include <unistd.h>
int main(void) {
	struct timeval tv;
	gettimeofday(&tv, NULL);
	printf("   tv_usec = %ld tv_sec = %ld\n", tv.tv_usec, tv.tv_sec);
	for(int i = 0; i < 4; i++){
        gettimeofday(&tv, NULL);
        printf("%d) tv_usec = %ld tv_sec = %ld\n", i, tv.tv_usec, tv.tv_sec);
        sleep(1);
	}
	return 0;
}

注:linux執行環境下sleep()函式需要新增標頭檔案#include <unistd.h>

執行結果:


相關推薦

c函式gettimeofday()函式獲取系統時間

gettimeofday     使用C語言編寫程式需要獲得當前精確時間(1970年1月1日到現在的時間),或者為執行計時,可以使用gettimeofday()函式。 函式原型:     int gettimeofday(struct timeval*tv, struct

Linux時間函式 gettimeofday() 函式使用方法

一.gettimeofday()函式的使用方法 1.簡介: 在C語言中可以使用函式gettimeofday()函式來得到時間。它的精度可以達到微妙 2.函式原型: #include<

Linux時間函式gettimeofday()函式使用方法

一.gettimeofday()函式的使用方法:1.簡介:在C語言中可以使用函式gettimeofday()函式來得到時間。它的精度可以達到微妙2.函式原型:#include<sys/time.h>int gettimeofday(struct  timeval*

Linux 時間函式 gettimeofday() 函式使用方法

一.gettimeofday() 函式的使用方法: 1.簡介: 在 C 語言中可以使用函式 gettimeofday() 函式來得到時間。它的精度可以達到微妙 2.函式原型: #include<sys/time.h> int gettimeofday(stru

C/C++ 獲取系統時間系統延遲函式呼叫

C或C++呼叫Windows系統函式 實現延時 或 獲取當前時間的處理。可在VC++或MInGW上用 C或C++實現。 1、 實現延時:  標頭檔案: #include<windows.h>  函式:  Sleep(int time);  //time單位為ms,

14.C語言中time函式和localtime獲取系統時間和日期

C語言中time函式和localtime獲取系統時間和日期可以通過time()函式來獲得計算機系統當前的日曆時間(Calendar Time),處理日期時間的函式都是以本函式的返回值為基礎進行運算。1. time 函式 返回1970-1-1, 00:00:00以來經過的秒數 

【Oracle中獲取系統時間的分秒函式

1.得到系統當前時間的年份 select to_char(sysdate, 'yyyy' ) from dual; --年 2.得到系統當前時間的月份 select to_char(sysdate,

c++筆記CArray函式

謹以此文獻給因為我菜雞同時裝了VS2013和2017導致vs各種衝突,以至於只能重灌系統的新電腦!哭泣.... CArray屬於MFC,是一個數組模板類。MFC的陣列類支援的陣列類似於常規陣列,可以存放任何資料型別。常規陣列在使用前必須將其定義成能夠容納所有可能需要的元素,即先確定大小,而M

linux C函式stat函式

1.函式功能: 通過檔名filename獲取檔案資訊,並儲存在buf所指的結構體stat中 2.函式原型 1)函式標頭檔案 #include <sys/stat.h> #include <unistd.h> 2)函式 int stat(const c

C函式memcpy 函式用法

分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow 也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!        

linux C函式access函式的用法【轉】

1.函式功能: 檢查呼叫程序是否可以對指定的檔案執行某種操作。 2.函式原型: 1)函式標頭檔案 #include <stdio.h> #include <unistd.h> 2)函式 int access(

C語言通過函式引數修改實參

#include <stdio.h> int x = 1; int y = 2; int *p = &x; void modify_1(int a) { a = 11;

linux c獲取系統時間

#include<iostream> #include <stdlib.h> #include <stdio.h> #include <sys/time.h> #include <unistd.h> int main(){ struc

C# 獲取系統時間時間格式

--DateTime 數字型  System.DateTime currentTime=new System.DateTime();  取當前年月日時分秒      currentTime=System.DateTime.Now;  取當前年     int 年=curren

Windows下C++獲取系統時間

使用GetLocalTime()函式 標頭檔案包含 Windows.h #include<Windows.h>  SYSTEMTIME sysTime;  GetLocalTime(&sysTime);

C函式memcpy 函式用法

標頭檔案:#include <string.h>memcpy() 用來複制記憶體,其原型為:    void * memcpy ( void * dest, const void * src, size_t num );memcpy() 會複製 src 所指的記憶體內容的前 num 個位元組到 d

C語言strrchr函式

【FROM MSDN && 百科】 原型:char *strrchr(const char *str, char c); #include<string.h> 找一個字元c在另一個字串str中末次出現的位置(也就是從str的右側開始查詢字

ZeroMQ介面函式 :zmq_getsockopt – 獲取ZMQ socket的屬性

翻譯:郝峰波 zmq_getsockopt(3)           ØMQ Manual -ØMQ/4.0.6 Name zmq_getsockopt – 獲取ZMQ socket的屬性 Synopsis int zmq_getsockopt (void *socket

C語言獲取系統時間方法

需要利用C語言的時間函式time和localtime,具體說明如下: 一、函式介面介紹: 1、time函式。 形式為time_t time (time_t *__timer); 其中time_t為time.h定義的結構體,一般為長整型。 這個函式會獲取

C語言利用函式交換兩個數的值

#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> void Swap(int *px,int *py) { int tmp = *p