Timer in C++11 with chrono: from nanoseconds to milliseconds

c++, c++11, performance

Solution

You can use `duration_cast`

chrono::duration_cast<chrono::milliseconds>
       (chrono::high_resolution_clock::now() - start).count()

Problem

I would like to know the milliseconds a function took. I am using chrono like this: ``` chrono::system_clock::time_point start = chrono::high_resolution_clock::now(); // big_function ... cout << "Function took: " << chrono::nanoseconds(chrono::high_resolution_clock::now() - start).count() << "nanosec" << endl; ``` This prints: `"Function took: 32423212131321 nanosec"` Is there any way to print milliseconds instead of nanosconds ?

Original source