Reputation: 21
I'm learning < chrono > right now and I don't understand one thing.
#include <iostream>
#include <chrono>
int main()
{
auto t1 = std::chrono::high_resolution_clock::now();
int a = 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 this case equals "0".
#include <iostream>
#include <chrono>
int main()
{
auto t1 = std::chrono::steady_clock::now();
int a = 0;
auto t2 = std::chrono::steady_clock::now();
auto result = t2 - t1;
std::cout << result.count() << std::endl;
return 0;
}
And in this case "result" equals about "5500".
Am I doing something wrong?
Upvotes: 1
Views: 1886
Reputation: 219448
No. It's just that different clocks can have different precisions, and timing something so short might not take more than 1 tick of a clock.
You can find out a clock's precision by inspecting clock::duration::period::num
and clock::duration::period::den
.
Additionally, some implementations of <chrono>
have had "quality of implementation" issues over the years. So your experience might change as you switch compilers, and even versions of the same compiler. Your experience will generally be better if you can work with the latest version of your compiler.
Here is a video tutorial for <chrono>
.
Upvotes: 5