#include <iostream>
#include <stdio.h>
#include <string>
#include <sys/time.h>
#include <unistd.h>
using namespace std;
int fa_getSysTime() {
struct timeval tv;
gettimeofday(&tv, NULL);
struct tm *pTime;
pTime = localtime(&tv.tv_sec);
char sTemp[30] = {0};
snprintf(sTemp, sizeof(sTemp), "%04d%02d%02d%02d%02d%02d%03d%03d",
pTime->tm_year + 1900, pTime->tm_mon + 1, pTime->tm_mday,
pTime->tm_hour, pTime->tm_min, pTime->tm_sec, tv.tv_usec / 1000,
tv.tv_usec % 1000);
string stringTime = (string)sTemp;
// start from minute
stringTime = stringTime.substr(10, 7);
int numTime = std::stoi(stringTime);
return numTime;
}
int main() {
cout << "当前时间:" << fa_getSysTime() << endl;
int start = fa_getSysTime();
int i = 0;
while (i < 20000) {
cout << "1233" << endl;
i++;
}
int end = fa_getSysTime();
cout << "start time is :" << start << " ; end time is : " << end << endl;
cout << "interval is " << end - start << endl;
return 0;
}
参考: https://blog.csdn.net/wcl199274/article/details/39693749