Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the default unit of time of c++'s chrono?

Tags:

c++

profiling

I used this code to measure the runtime of my program from this answer

auto start = std::chrono::system_clock::now();

/* do some work */

auto end = std::chrono::system_clock::now();
auto elapsed = end - start;
std::cout << elapsed.count() << '\n';

I only needed to measure the runtime to look for a trend but I am kind of curious as to what unit that is.

like image 235
user224266 Avatar asked Sep 02 '25 04:09

user224266


1 Answers

You can easily check by your self, with this code:

using Clock = std::chrono::system_clock;
using Duration = Clock::duration;
std::cout << Duration::period::num << " , " << Duration::period::den << '\n';

On my system it prints:

 1 , 1000000000

That is, a period in terms of nanoseconds (i.e. 10−9s).

like image 196
BiagioF Avatar answered Sep 04 '25 18:09

BiagioF