1

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 ?

bholanath
  • 1,529
  • 1
  • 18
  • 31
  • 2
    First of all I would check if you get nanosecond precision with [`clock_gettime`](http://stackoverflow.com/q/538609/2802841), if you don't then chances are your system doesn't support nanosecond precision. – user2802841 Mar 19 '14 at 11:06
  • @user2802841 thanks for reply. I am getting nanosecond precision there.The problem is I want to print the current time with nanosecond precision and for that I have used the Boost timer. It only print with microsec precision. – bholanath Mar 19 '14 at 11:18
  • @user2802841 Is there any other timer which I can use to print current time in nanoseconds precision?. – bholanath Mar 19 '14 at 11:24
  • Precision maybe, accuracy no. Obviously, the value will be grossly out-of-date by the time the cout call has completed. It will take a couple of ns for the light from your display to reach your eyes. – Martin James Mar 19 '14 at 12:03
  • If you want to operate with nano - ratio must be: nano std::ratio<1, 1000000000> micro std::ratio<1, 1000000> – AlexBG Mar 19 '14 at 12:09
  • @AlexBG how to use this ? any example code to use will be great. – bholanath Mar 19 '14 at 13:19

2 Answers2

0

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

Community
  • 1
  • 1
tejas
  • 1,465
  • 13
  • 31
  • I use Intel(R) Core(TM) i3 CPU 550 @ 3.20GHz and output of program is: 1000000000 sec = 0: micro = 543327: nano = 543327255: – AlexBG Mar 19 '14 at 12:00
  • my system is Intel(R) Core(TM) i3-2100 CPU @ 3.10GHz and output is 1000000 sec, micro = 616303 nanosec = 616303000 – bholanath Mar 19 '14 at 13:18
0

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;
}
AlexBG
  • 364
  • 1
  • 6
  • to my surprise although my system produces in nanosecond precision with clock_gettime , but not with this chrono, which is much more high precision timer. – bholanath Mar 19 '14 at 14:42