C++ Cross-Platform High-Resolution Timer

前端 未结 14 2142
不知归路
不知归路 2020-11-22 12:45

I\'m looking to implement a simple timer mechanism in C++. The code should work in Windows and Linux. The resolution should be as precise as possible (at least millisecond a

相关标签:
14条回答
  • 2020-11-22 13:26

    I found this which looks promising, and is extremely straightforward, not sure if there are any drawbacks:

    https://gist.github.com/ForeverZer0/0a4f80fc02b96e19380ebb7a3debbee5

    /* ----------------------------------------------------------------------- */
    /*
    Easy embeddable cross-platform high resolution timer function. For each 
    platform we select the high resolution timer. You can call the 'ns()' 
    function in your file after embedding this. 
    */
    #include <stdint.h>
    #if defined(__linux)
    #  define HAVE_POSIX_TIMER
    #  include <time.h>
    #  ifdef CLOCK_MONOTONIC
    #     define CLOCKID CLOCK_MONOTONIC
    #  else
    #     define CLOCKID CLOCK_REALTIME
    #  endif
    #elif defined(__APPLE__)
    #  define HAVE_MACH_TIMER
    #  include <mach/mach_time.h>
    #elif defined(_WIN32)
    #  define WIN32_LEAN_AND_MEAN
    #  include <windows.h>
    #endif
    static uint64_t ns() {
    static uint64_t is_init = 0;
    #if defined(__APPLE__)
        static mach_timebase_info_data_t info;
        if (0 == is_init) {
            mach_timebase_info(&info);
            is_init = 1;
        }
        uint64_t now;
        now = mach_absolute_time();
        now *= info.numer;
        now /= info.denom;
        return now;
    #elif defined(__linux)
        static struct timespec linux_rate;
        if (0 == is_init) {
            clock_getres(CLOCKID, &linux_rate);
            is_init = 1;
        }
        uint64_t now;
        struct timespec spec;
        clock_gettime(CLOCKID, &spec);
        now = spec.tv_sec * 1.0e9 + spec.tv_nsec;
        return now;
    #elif defined(_WIN32)
        static LARGE_INTEGER win_frequency;
        if (0 == is_init) {
            QueryPerformanceFrequency(&win_frequency);
            is_init = 1;
        }
        LARGE_INTEGER now;
        QueryPerformanceCounter(&now);
        return (uint64_t) ((1e9 * now.QuadPart)  / win_frequency.QuadPart);
    #endif
    }
    /* ----------------------------------------------------------------------- */-------------------------------- */
    
    0 讨论(0)
  • 2020-11-22 13:27

    The StlSoft open source library provides a quite good timer on both windows and linux platforms. If you want it to implement on your own, just have a look at their sources.

    0 讨论(0)
  • 2020-11-22 13:27

    I am not sure about your requirement, If you want to calculate time interval please see thread below

    Calculating elapsed time in a C program in milliseconds

    0 讨论(0)
  • 2020-11-22 13:32

    STLSoft have a Performance Library, which includes a set of timer classes, some that work for both UNIX and Windows.

    0 讨论(0)
  • 2020-11-22 13:33

    I highly recommend boost::posix_time library for that. It supports timers in various resolutions down to microseconds I believe

    0 讨论(0)
  • 2020-11-22 13:34

    If one is using the Qt framework in the project, the best solution is probably to use QElapsedTimer.

    0 讨论(0)
提交回复
热议问题