計算時間間隔

2021-07-02 15:42:35 字數 1304 閱讀 3892

time_t 獲得時間只能精確到

秒,clock_t 獲得時間能夠精確到毫秒

#include clock_t start,ends;

start=clock();

system("pause");

ends=clock();

cout<

code highlighting produced by actipro codehighlighter (freeware)

-->#include #include #include #include #include #include #include using namespace std;

int _tmain(int argc, _tchar* argv)

time(&end);

cout << "採用計時方式一(精確到秒):迴圈語句執行了:" << (end-start) << "秒" << endl;

//計時方式二

struct timeb starttime , endtime;

ftime(&starttime);

for(int i=0; i < numeric_limits::max(); i++)

ftime(&endtime);

cout << "採用計時方式二(精確到毫秒):迴圈語句執行了:" << (endtime.time-starttime.time)*1000 + (endtime.millitm - starttime.millitm) << "毫秒" << endl;

//計時方式三

clock_t startctime , endctime;

startctime = clock(); //clock函式返回cpu時鐘計時單元(clock tick)數,還有乙個常量表示一秒鐘有多少個時鐘計時單元,可以用clock()/clocks_per_sec來求取時間

for(int i=0; i < numeric_limits::max(); i++)

endctime = clock();

cout << "採用計時方式三(好像有些延遲,精確到秒):迴圈語句執行了:" << double((endctime-startctime)/clocks_per_sec) << "秒" << endl;

cout << "綜合比較上述三種種計時方式,方式二能夠精確到毫秒級別,比方式一和三都較好。此外在windows api中還有其他的計時函式,用法都大同小異,在此就不做介紹了。" << endl;

system("pause");

return 0;

}

計算時間間隔

題目描述 讀入兩個用 時 分 秒 表示的時間點,計算以秒為單位的時間間隔。輸入輸入有兩行,每行是乙個用 時 分 秒 表示的時間點。測試資料保證第二個時間點晚於第乙個時間點。輸出輸出乙個整數,表示時間間隔的秒數。樣例輸入 08 00 0009 00 00樣例輸出 3600解題思路 直接兩個時間的時分秒...

C 時間間隔計算

c 時間比較 c 時間間隔計算 c 事件比較直接可以用減法,即timespan ts1 datetime.now datetime.now.adddays 1 它返回的是乙個時間間隔。它有如下屬性 days 獲取當前 timespan 結構所表示的時間間隔的天數部分。hours 獲取當前 times...

VB計算時間間隔

datediff interval,date1,date2 firstdayofweek firstweekofyear 引數含義 interval 必要。字串表示式,表示用來計算date1 和 date2 的時間差的時間間隔 date1 date2 必要 variant date 計算中要用到的兩...