c++ - std::chrono::high_resolution_clock - time measurement -


i'm learning < chrono > right , don't understand 1 thing.

#include <iostream> #include <chrono>  int main() {     auto t1 = std::chrono::high_resolution_clock::now();     int = 0;     auto t2 = std::chrono::high_resolution_clock::now();     auto result = t2 - t1;     std::cout << result.count() << std::endl;     return 0; } 

value of "result" in case equals "0".

#include <iostream> #include <chrono>  int main() {     auto t1 = std::chrono::steady_clock::now();     int = 0;     auto t2 = std::chrono::steady_clock::now();     auto result = t2 - t1;     std::cout << result.count() << std::endl;     return 0; } 

and in case "result" equals "5500".

am doing wrong?

no. it's different clocks can have different precisions, , timing short might not take more 1 tick of clock.

you can find out clock's precision inspecting clock::duration::period::num , clock::duration::period::den.

additionally, implementations of <chrono> have had "quality of implementation" issues on years. experience might change switch compilers, , versions of same compiler. experience better if can work latest version of compiler.

here video tutorial <chrono>.


Comments