I am trying to compare the time elapsed to execute a piece of code with a fixed integer.
For example:
auto startProcessingTime = chrono::high_resolution_clock::now();
chrono::duration<int> timeDiff = (chrono::duration_cast<chrono::seconds>(chrono::high_resolution_clock::now() - startProcessingTime));
if (timeDiff > 12) {
// DO SOMETHING
continue;
}
However on running this I get the following error:
Invalid operands to binary expression ('chrono::duration' and 'int')
How can I convert timeDiff
to an integer?
I have also tried:
chrono::seconds s = chrono::duration_cast<chrono::seconds>(timeDiff);
However,
Invalid operands to binary expression ('chrono::seconds' (aka 'duration') and 'int')
Any help would be greatly appreciated.
You need to tell it what 12
means. Is it seconds? Milliseconds? So either cast it:
chrono::seconds(12)
Or (my favorite) make it a chrono literal. If you mean 12 seconds, then:
using namespace std::chrono_literals;
// ...
if (timeDiff > 12s) {
If it's milliseconds:
if (timeDiff > 12ms) {
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With