#include <boost/chrono.hpp>
int main()
{
boost::chrono::high_resolution_clock::time_point start = boost::chrono::high_resolution_clock::now();
//burn some time
boost::chrono::nanoseconds ns = boost::chrono::high_resolution_clock::now() - start;
auto val = ns.count();
}
Under Windows 7 64 bits and boost 1.48 could I be sure that the above code will well return me a time in nanosecond/with nanosecond precision ?
EDIT: After doing some test :
#include <boost/chrono.hpp>
#include <windows.h>
double PCFreq = 0.0;
__int64 CounterStart = 0;
void StartCounter()
{
LARGE_INTEGER li;
if(!QueryPerformanceFrequency(&li))
std::cout << "QueryPerformanceFrequency failed!\n";
PCFreq = double(li.QuadPart)/1000.0;
QueryPerformanceCounter(&li);
CounterStart = li.QuadPart;
}
double GetCounter()
{
LARGE_INTEGER li;
QueryPerformanceCounter(&li);
return double(li.QuadPart-CounterStart)/PCFreq;
}
int main()
{
boost::chrono::high_resolution_clock::time_point start = boost::chrono::high_resolution_clock::now();
Sleep(1000);
boost::chrono::nanoseconds sec = boost::chrono::high_resolution_clock::now() - start;
std::cout << sec.count()/1000000. <<'\n';
StartCounter();
Sleep(1000);
std::cout << GetCounter() <<'\n';
std::cin.ignore();
}
both method return approximatively the same result.Something strange about the above code is that first method invoqued in the main will always return the best result, switch boost in second position and GetCounter will be the best (i.e: more closer to Sleep duration)