Mingw-packages: G++ high_resolution_clock Resolution

Created on 18 Mar 2019  路  4Comments  路  Source: msys2/MINGW-packages

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?

Most helpful comment

All 4 comments

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.

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';
  }
}
Was this page helpful?
0 / 5 - 0 ratings