在window環境下:
1.精確到毫秒
#include "stdafx.h"
#include <windows.h>
#include <iostream>
using namespace std;
int main(int argc, _TCHAR* argv[])
{
DWORD time_start, time_end;
/* 獲取開始時間 */
time_start = GetTickCount(); //從操作系統啟動經過的毫秒數
Sleep(3000);
time_end = GetTickCount();
cout << "Time = " << (time_end - time_start) << "ms\n ";
system("pause");
return 0;
}
2.精確到秒

#include <windows.h>
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
SYSTEMTIME st = { 0 };
GetLocalTime(&st); //獲取當前時間 可精確到ms
printf("%d-%02d-%02d %02d:%02d:%02d\n",
st.wYear,
st.wMonth,
st.wDay,
st.wHour,
st.wMinute,
st.wSecond);
}