I'm running into an issue with std::chrono::high_resolution_clock having a resolution of 1 ms - which is unacceptably large for what I am attempting to do; and I suspect probably unacceptable for most intended uses of a high resolution clock. I also believe it is lying about its actual resolution.
After researching the issue, I found this https://github.com/electronicarts/EASTL/issues/50 which also references https://sourceforge.net/p/mingw-w64/mailman/message/31672495/ .
I attached a small .cpp file from the proof of concept I was writing when I encountered this problem that illustrates the issue. generatorid.txt
I am going to investigate using Boost as that second link suggested, but are there any other workarounds or suggestions for this problem? Or thoughts on a fix?
No workarounds as far as I know. As a general rule, if you don't want surprises always use boost instead of libstdc++ on Windows.
Speaking of workarounds, I just found https://stackoverflow.com/questions/15751308/measuring-time-results-in-return-values-of-0-or-0-001/15755865#15755865
I don't know how convenient it is.
Adapted from the SO answer referenced above:
#include <iostream>
#include <vector>
#include <chrono>
#include <windows.h>
struct my_clock
{
typedef double rep;
typedef std::ratio<1> period;
typedef std::chrono::duration<rep, period> duration;
typedef std::chrono::time_point<my_clock> time_point;
static const bool is_steady = false;
static time_point now()
{
static const long long frequency = init_frequency();
LARGE_INTEGER t;
QueryPerformanceCounter(&t);
return time_point(duration(static_cast<rep>(t.QuadPart)/frequency));
}
private:
static long long init_frequency()
{
LARGE_INTEGER f;
QueryPerformanceFrequency(&f);
return f.QuadPart;
}
};
// typedef std::chrono::high_resolution_clock C;
typedef my_clock C;
int main() {
for (unsigned long long size = 1; size < 10000000; size *= 10) {
auto start = C::now();
std::vector<int> v(size, 42);
auto end = C::now();
auto elapsed = end - start;
std::cout << size << ": " << elapsed.count() << '\n';
}
}
Yep, I reported the upstream PR 63400. I worked around the problem by using std::chrono::steady_clock instead.
https://github.com/FrankHB/YSLib/blob/master/YFramework/source/YCLib/Timer.cpp#L95
https://github.com/FrankHB/YSLib/blob/master/YFramework/include/YSLib/Core/YClock.h#L45
Some additional notes:
https://github.com/FrankHB/YSLib/blob/master/doc/Workflow.Annual2014.txt#L648
Most helpful comment
Yep, I reported the upstream PR 63400. I worked around the problem by using
std::chrono::steady_clockinstead.https://github.com/FrankHB/YSLib/blob/master/YFramework/source/YCLib/Timer.cpp#L95
https://github.com/FrankHB/YSLib/blob/master/YFramework/include/YSLib/Core/YClock.h#L45
Some additional notes:
https://github.com/FrankHB/YSLib/blob/master/doc/Workflow.Annual2014.txt#L648