How to get the execution time of a code fragment in nanoseconds using chrono? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 6

How to get the execution time of a code fragment in nanoseconds using chrono?

I have a function and I wish to get the time taken by the compiler to execute it (the runtime). But as the process can be fast, I wish to get the time in nanoseconds, to see all changes. I tried using chrono, like this: using namespace chrono; auto start = high_resolution_clock::now(); /* code block */ auto end = high_resolution_clock::now(); cout<<duration_cast<nanoseconds>(end - start) . count(); However, I just get a 0. Please help me correct my code!

14th Sep 2017, 2:59 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
2 Answers
14th Sep 2017, 3:17 PM
jay
jay - avatar
+ 7
@Ace Sir, why is it that with a steady clock like in your program, I could get a non-zero value but with a high resolution clock, I couldn't?
14th Sep 2017, 3:50 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar