How to measure time in nanoseconds with C++

1 Answer

0 votes
#include <iostream>
#include <chrono>

int main() {
    auto start = std::chrono::high_resolution_clock::now();
    
    if (1)
        std::cout << "True" << '\n';
    
    auto end = std::chrono::high_resolution_clock::now();
    
    auto time_in_nanoseconds = std::chrono::duration_cast<std::chrono::nanoseconds>(end - start).count();
    
    std::cout << time_in_nanoseconds << " [ns]";
}




/*
run:

True
24993 [ns]

*/

 



answered Feb 22, 2022 by avibootz

Related questions

1 answer 92 views
92 views asked Mar 14, 2025 by avibootz
1 answer 126 views
1 answer 262 views
1 answer 97 views
97 views asked Oct 17, 2024 by avibootz
2 answers 130 views
1 answer 175 views
175 views asked Feb 21, 2022 by avibootz
...