Linux和Windows毫秒时间 [英] Linux and Windows milisecond time
问题描述
我想得到系统的毫秒时间(我不在乎,如果它是实时,我希望它尽可能准确)。这是一个好的方法吗?
#ifdef WIN32
unsigned long long freq;
unsigned long long get_ms_time(){
LARGE_INTEGER t;
QueryPerformanceCounter(& t);
return t.QuadPart / freq;
}
#else
unsigned long long get_ms_time(){
struct timespec t;
clock_gettime(CLOCK_MONOTONIC,& t);
return t.tv_sec * 1000 + t.tv_nsec / 1000000;
}
#endif
如何将此值封装到一个signed int ?我尝试这样做,我得到这样的负值(在Linux上,我不知道在Windows上):
〜start
-2083002438
〜15秒后...
-2082987440
〜15秒后...
-2082972441
我会这样。
〜start
X
〜15秒后...
X + 14998
〜15秒后...
X + 29997
其中X是正数。 (我希望输出为正且增加)
非常感谢!
我在代码中这样做...
timespec specStart,specStop;
//以秒为单位获取开始时间(UNIX时间戳)...
clock_gettime(CLOCK_MONOTONIC_RAW,& startTime);
int startTimeInt = startTime.tv_sec;
std :: cout<< 开始时间:< startTimeInt<< std :: endl;
...
//以秒为单位获取停止时间(UNIX时间戳)...
clock_gettime(CLOCK_MONOTONIC_RAW,& stopTime);
int stopTimeInt = stopTime.tv_sec;
std :: cout<< 停止时间:< stopTimeInt<< std :: endl;
//从停止时间到开始时间的时间差异
unsigned long long timeStart = specStart.tv_sec * 1000000000 + specStart.tv_nsec;
unsigned long long timeStop = specStop.tv_sec * 1000000000 + specStop.tv_nsec;
unsigned long long timeDelta = timeStio - timeStart; //时间差,单位为纳秒。
int microSec = timeDelate / 1000;
int mSec = timeDelta / 1000000;
int sec = timeDelta / 1000000000;
std :: cout<< time diff:<< std :: endl
<< sec<< s< std :: endl
<< msec < ms< std :: endl
<< microSec < μs< std :: endl
<< timeDelta<< ns< std :: endl;
I want to get the miliseconds time of a system (I don't care if it's the real time, I want it to be as accurate as possible). Is this a good method to do it?
#ifdef WIN32
unsigned long long freq;
unsigned long long get_ms_time() {
LARGE_INTEGER t;
QueryPerformanceCounter(&t);
return t.QuadPart / freq;
}
#else
unsigned long long get_ms_time() {
struct timespec t;
clock_gettime(CLOCK_MONOTONIC, &t);
return t.tv_sec * 1000 + t.tv_nsec / 1000000;
}
#endif
How can I wrap this value to a signed int? I tried doing this and I get negative values like this (on Linux, I don't know on Windows):
~ start
-2083002438
~ 15 seconds after..
-2082987440
~ 15 seconds after..
-2082972441
I would something like this. ~ start X ~ 15 seconds after.. X + 14998 ~ 15 seconds after.. X + 29997
Where X is a positive number. (I want the output positive and increasing)
Thank you very much!
I do something like this in my code...
timespec specStart, specStop;
// Get start time ( UNIX timestamp ) in seconds...
clock_gettime( CLOCK_MONOTONIC_RAW, &startTime );
int startTimeInt = startTime.tv_sec;
std::cout << "start time : " << startTimeInt << std::endl;
...
// Get stop time ( UNIX timestamp ) in seconds...
clock_gettime( CLOCK_MONOTONIC_RAW, &stopTime );
int stopTimeInt = stopTime.tv_sec;
std::cout << "stop time : " << stopTimeInt << std::endl;
// Get time diff from stop time to start time
unsigned long long timeStart = specStart.tv_sec * 1000000000 + specStart.tv_nsec;
unsigned long long timeStop = specStop.tv_sec * 1000000000 + specStop.tv_nsec;
unsigned long long timeDelta = timeStio - timeStart; // Time diff in nanoseconds.
int microSec = timeDelate / 1000;
int mSec = timeDelta / 1000000;
int sec = timeDelta / 1000000000;
std::cout << "time diff : " << std::endl
<< sec << " s" << std::endl
<< msec << " ms" << std::endl
<< microSec << " µs" << std::endl
<< timeDelta << " ns" << std::endl;
这篇关于Linux和Windows毫秒时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!