How to make my system support nano seconds precisi

2019-05-30 06:44发布

问题:

When I run the code from this page high_precision_timer, I got to know my system only support microsecond precision.

As per the document,

cout << chrono::high_resolution_clock::period::den << endl;

Note, that there isn’t a guarantee how many the ticks per seconds it has, only that it’s the highest available. Hence, the first thing we do is to get the precision, by printing how many many times a second the clock ticks. My system provides 1000000 ticks per second, which is a microsecond precision.

I am also getting exactly the same value 1000000 ticks per second . That means my system is also support microseconds precision.

Everytime I run any program , I always get value xyz microsecond and xyz000 nanosec . I think the above non-support of my system to nanosec may be the reason.

Is there any way to make my system nanosec supportive ?

回答1:

It's not an answer. I cannot print long message in comment. I just test your example. And my system output result was: chrono::high_resolution_clock::period::den = 1000000000. My system provides 1000000000 ticks per second, which is a nanosecond precision. Not 1000000 (microseconds). Your system provides 1000000 ticks per second, which is a microsecond precision. So, I don't know how to help you. Sorry.

#include <iostream>
#include <chrono>
using namespace std;

int main()
{
    cout << chrono::high_resolution_clock::period::den << endl;
    auto start_time = chrono::high_resolution_clock::now();
    int temp;
    for (int i = 0; i< 242000000; i++)
        temp+=temp;
    auto end_time = chrono::high_resolution_clock::now();
    cout <<"sec = "<<chrono::duration_cast<chrono::seconds>(end_time - start_time).count() << ":"<<std::endl;
    cout <<"micro = "<<chrono::duration_cast<chrono::microseconds>(end_time - start_time).count() << ":"<<std::endl;
    cout <<"nano = "<<chrono::duration_cast<chrono::nanoseconds>(end_time - start_time).count() << ":"<<std::endl;
    return 0;
}


回答2:

Consider this, Most processors today operate at a frequency of about 1 to 3 GHz i.e. say 2 * 10^9 Hz. which means 1 tick every 0.5 nano seconds at the processor level. so i would guess your chances are very very slim.

Edit: though the documentation is still sparse for this I remember reading that it accesses the RTC of the CPU(not sure), whose frequency is fixed. Also as an advice i think measuring performance in nano second has little advantage compared to measuring in micro sec ( unless its for medical use ;) ).

and take a look at this question and its answer. I think it can make more sense HPET's frequency vs CPU frequency for measuring time