Accurate C / C ++ clocks on a multi-core processor with automatic overclocking?

I looked at several topics to try and get some ideas on how to make a reliable watch with C or C ++. However, I also saw that some functions used processor ticks and ticks per second to calculate the final result, which, I think, could be a problem for a processor with automatic overclocking, like the one I have. I also saw one of them reset after a while, thus not very reliable.

The idea is to make (preferably cross-platform) watches, for example, in a game, with an accuracy of better than a second, in order to be able to add elapsed time to the “current session” with the saved time at the end of the program. This would calculate the time spent on a console game in which there are no game hours, and in the long run, perhaps integrate it into real PC games.

It should be able to start without too much or the entire processor time (or single-core time for multi-core processors), since it would be nice to use all these resources only for the clock, as well as for a system with automatic overclocking (which otherwise could lead to inaccurate results).

The program that I would like to implement this function now looks like this, but I can transcode it into C (since I need to return to learning how to write code in C ++):

#include <iostream>
#include <cstdlib>

using namespace std;

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

On the other hand, I still need to get rid of the PAUSE function, which depends on Windows, but I think that you can take care of it with the simple "while (char! = '\ N')".

What I already looked through:

(Edit: , , C:

, , , Boost SDL2, .

TL; DR: - , C/++, / ?

.

+4
1

, std::chrono::high_resolution_clock , . , CPU.

, , . , , . :

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";

, , , (RDTSC Intel). , .

+1

Source: https://habr.com/ru/post/1691353/


All Articles