C語言中獲取時間記錄

2021-07-31 07:51:34 字數 1169 閱讀 1668

最近在網路上找了乙個cpu和gpu矩陣乘法對比的demo(

在linux中執行,一直報錯

error: identifier "gettickcount" is undefined

這是因為gettickcount是windows中的函式,需要引入標頭檔案windows.h,當然,在linux中這種方法並不適用。這就需要我們了解gettickcount的作用,找到windows中gettickcount()在linux中的替代函式。

經過查詢得到,gettickcount是記錄時間的,返回自裝置啟動後的毫秒數,用法為

start:=gettickcount;   

...//執行耗時的操作

stop:=gettickcount;

timeused:=(stop-start)/1000; //使用了***秒

(參考

這就需要找到linux中記錄時間的方法。c語言中有多種方法可以實現,例如clock()、times()、clock_gettime()、gettimofday(),但是不同方法是有差別的

clock()函式的精確度是10毫秒(ms)

times()函式的精確度是10毫秒(ms)

gettimofday()函式的精確度是微秒(μs)

clock_gettime()函式的計量單位為十億分之一,也就是納秒(ns)

參考我使用的方法是clock_gettime(),用法為

struct timespec tpstart;

struct timespec tpend;

long timedif;

//unsigned int tick1 = gettickcount();

clock_gettime(clock_monotonic, &tpstart);

...//執行耗時的操作

clock_gettime(clock_monotonic, &tpend);

timedif = 1000*(tpend.tv_sec - tpstart.tv_sec) + (tpend.tv_nsec - tpstart.tv_nsec)/1000000;

printf("use time : %ldms\n",timedif);

C語言中time函式獲取系統時間

可以通過time 函式來獲得計算機系統當前的日曆時間 calendar time 處理日期時間的函式都是以本函式的返回值為基礎進行運算。其原型為 time t time time t t include int main void 執行的結果與當時的時間有關,我當時執行的結果是 the calend...

C語言中時間總結

在c語言涉及中經常需要定時觸發事件,涉及到獲取系統時間,其結構體型別有多種。unix linux系統下有以下幾種時間結構 1 time t 型別 長整型,一般用來表示從1970 01 01 00 00 00時以來的秒數,精確度 秒 由函式time 獲取 該型別定義在標頭檔案 usr include ...

C語言中的時間轉換

c語言關於時間轉換的函式在標頭檔案和中,其中中包含了各種操作時間和日期的函式,中包換結構體timeb,可以用來獲取ms級別的時間 可利用標頭檔案中的兩個結構體獲取ms級的執行時間,統計程式的時間複雜度 struct timeb include include using namespace std ...