1. 程式人生 > 其它 >【C++】秒級時間戳,毫秒級時間戳

【C++】秒級時間戳,毫秒級時間戳

時間戳,秒級

測試程式碼:

#include <iostream>
#include <time.h>
#include <windows.h>
using namespace std;

int main()
{
    //------當前的時間戳(秒)--------
    time_t t;
    time(&t);
    cout << t << endl;
    Sleep(1200);
    time(&t);
    cout << t << endl;

    system(
"pause"); return 0; }

結果:


時間戳,毫秒級

測試程式碼:

#include <iostream>
#include<sys/timeb.h>
using namespace std;

long long systemtime()
{
    timeb t;
    ftime(&t);
    return t.time * 1000 + t.millitm;
}
int main()
{
    //------當前的時間戳(豪秒)--------
    while (1)
    {
        long long timeA = systemtime();
        cout 
<< timeA << endl;
     //system("cls"); } system(
"pause"); return 0; }

結果:


毫秒級時間間隔

測試程式碼:

#include <iostream>
#include<time.h>
#include <windows.h>
using namespace std;


int main()
{
    //------時間間隔(豪秒)--------
    clock_t start, end;
    start = clock();
    Sleep(
50); //windows API 毫秒 end = clock(); cout << end - start << "毫秒" << endl; system("pause"); return 0; }

結果:

相關部落格:【C++】獲取當前的日期和時間(多種格式)