Accurate C/C++ clock on a multi

I have looked into several topics to try to get some ideas on how to make a reliable clock with C or C++. However, I also saw some functions used the processor's ticks and ticks per second to calculate the end result, which I think could be a problem on a CPU with auto-overclock like the one I have. I also saw one of them reset after a while, thus is not really reliable.

The idea is to make a (preferably cross-platform) clock like an in-game one, with a precision better than a second in order to be able to add the elapsed time in the "current session" with the saved time at the end of the program. This would be to count the time spent on a console game that does not have an in-game clock, and on the long run to perhaps integrate it to actual PC games.

It should be able to run without taking too much or all of the CPU's time (or a single core's time for multi-core CPUs) as it would be quite bad to use all these resources just for the clock, and also on systems with auto-overclock (which could otherwise cause inaccurate results).

The program I would like to implement this feature into currently looks like this, but I might re-code it in C (since I have to get back to learning how to code in C++):

#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
    cout << "In game" << endl;
    system("PAUSE");
    return 0;
}

On a side-note, I still need to get rid of the PAUSE feature which is Windows-specific, but I think that can be taken care of with a simple "while (char != 'n')" loop.

What I have already skimmed through:

  • Using clock() to measure execution time
  • Calculating elapsed time in a C program in milliseconds
  • Time stamp in the C programming language
  • Execution time of C program
  • C: using clock() to measure time in multi-threaded programs
  • Is gettimeofday() guaranteed to be of microsecond resolution?
  • How to measure time in milliseconds using ANSI C?
  • C++ Cross-Platform High-Resolution Timer
  • Timer function to provide time in nano seconds using C++
  • How to measure cpu time and wall clock time?
  • How can I measure CPU time and wall clock time on both Linux/Windows?
  • how to measure time?
  • resolution of std::chrono::high_resolution_clock doesn't correspond to measurements
  • C++ How to make timer accurate in Linux
  • http://gameprogrammingpatterns.com/game-loop.html
  • clock() accuracy
  • std::chrono doesn't seem to be giving accurate clock resolution/frequency
  • clock function in C++ with threads
  • (Edit: Extra research, in particular for a C implementation:

  • Cross platform C++ High Precision Event Timer implementation (no real answer)
  • Calculating Function time in nanoseconds in C code (Windows)
  • How to print time difference in accuracy of milliseconds and nanoseconds? (could be the best answer for a C implementation)
  • How to get duration, as int milli's and float seconds from <chrono>? (C++ again) )
  • The problem is that it is not clear whether some of the mentioned methods, like Boost or SDL2, behave properly with auto-overclock in particular.

    TL;DR : What cross-platform function should I use to make an accurate, sub-second precise counter in C/C++ that could work on multi-core and/or auto-overclocking processors please?

    Thanks in advance.


    The std::chrono::high_resolution_clock seems to be what you are looking for. On most modern CPUs it is going to be steady monotonically increased clock which would not be affected by overclocking of the CPU.

    Just keep in mind that it can't be used to tell time. It is only good for telling the time intervals, which is a great difference. For example:

    using clock = std::chrono::high_resolution_clock;
    auto start = clock::now();
    perform_operation();
    auto end = clock::now();
    auto us = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();
    std::cout << "Operation took " << us << " microseconds.n";
    

    If the clock checking itself is a performance-sensitive operation, you will have to resort to platform-specific tricks, of which the most popular is reading CPU tick counter directly ( RDTSC in Intel family). This is very fast, and on modern CPUs very accurate way of measuring time intervals.

    链接地址: http://www.djcxy.com/p/85720.html

    上一篇: 使用const non优化pow()

    下一篇: 准确的C / C + +时钟在一个多