Bruice
Bruice

Reputation: 663

What does std::chrono::duration::count return?

#include <iostream>
#include <chrono>


int main()
{
    std::chrono::time_point<std::chrono::system_clock> start_time, end_time;
    start_time = std::chrono::system_clock::now();
    //do something
    for (int i = 0; i<100000; ++i)
        std::cout << "";

    end_time = std::chrono::system_clock::now();
    auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time);
    std::cout << "\n" << duration.count();


    return 0;
}

I haven't seen it clearly in the docs, so what to check, that in this code spnippet when I got 22 printed to console it means 22 milliseconds. And if to write std::chrono::duration_cast<std::chrono::microseconds> and I receive 22323 it's microseconds, and so on. Correct?

Upvotes: 0

Views: 1751

Answers (1)

eerorika
eerorika

Reputation: 238351

What does std::chrono::duration::count return?

It returns the number of "ticks" in the duration. The returned type is determined by the first template argument of std::chrono::duration and the length of time that a "tick" represents is determined by the second template argument.

Upvotes: 1

Related Questions