
正文
【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++】获取当前的日期和时间(多种格式)








