我有以下代碼:使用boost :: posix_time :: microsec_clock進行測量的錯誤超過10微秒?
long long unsigned int GetCurrentTimestamp()
{
LARGE_INTEGER res;
QueryPerformanceCounter(&res);
return res.QuadPart;
}
long long unsigned int initalizeFrequency()
{
LARGE_INTEGER res;
QueryPerformanceFrequency(&res);
return res.QuadPart;
}
//start time stamp
boost::posix_time::ptime startTime = boost::posix_time::microsec_clock::local_time();
long long unsigned int start = GetCurrentTimestamp();
// ....
// execution that should be measured
// ....
long long unsigned int end = GetCurrentTimestamp();
boost::posix_time::ptime endTime = boost::posix_time::microsec_clock::local_time();
boost::posix_time::time_duration duration = endTime - startTime;
std::cout << "Duration by Boost posix: " << duration.total_microseconds() <<std::endl;
std::cout << "Processing time is " << ((end - start) * 1000000/initalizeFrequency())
<< " microsec "<< std::endl;
這段代碼的結果是
Duration by Boost posix: 0
Processing time is 24 microsec
爲什麼有這麼大的差異? Boost吸收儘可能多,因爲它應該測量微秒,但它會測量微秒的十微秒誤差?
這只是意味着boost :: posix_time :: microsec_clock是一個謊言:)) – Narek
你不能說這是一個謊言。讓值攜帶微秒是有意義的,因爲這些值確實攜帶了微秒範圍內的信息。但粒度不在該範圍內。一個典型的情況是時鐘和速度乘以156,250 100納秒。這是15.625毫秒,換句話說是15毫秒和625微秒。 **但時鐘有這麼多。它具有這樣的粒度。** – Arno