std::chrono::milliseconds .count() returns in microseconds?

"= std::chrono::duration_cast"
duration_cast std chrono microseconds
chrono::duration::count return type
std chrono durationcast
std chrono convert duration to milliseconds

I am trying to log milliseconds of time that has elapsed over a period of time.

I have a class like this

// class member declarations
class MyClass {

    std::chrono::high_resolution_clock::time_point   m_start;
    std::chrono::system_clock::duration              m_elapsed;

I have 2 methods in the class. One is called from main that is func1CalledFromMainThread.

// Class methods
using namespace std::chrono;
void MyClass::func1CalledFromMainThread() {

    m_start = std::chrono::high_resolution_clock::now();

And another one that is func2CalledFromADifferentThread is called from a different thread

void MyClass::func2CalledFromADifferentThread() {

    // after some time following line of code runs from a different thread
    auto end = high_resolution_clock::now();

    m_elapsed = duration_cast<milliseconds>(end - m_start);
    std::cout << "Elapsed time in milliseconds is " << m_elapsed.count()/1000 << std::endl;

The issue is in the cout logging. I see that I have to divide by 1000 to get milliseconds out of m_elapsed. Doesn't count return the count of std::chrono::milliseconds here? Why should I have to divide by 1000 here? Does count() return always in microseconds or am I doing a mistake?

count returns the number of ticks of the type on which you invoke it. If you wrote this:

duration_cast<milliseconds>(end - m_start).count()

it would correctly give you the number of milliseconds. However, you're not storing the result in std::chrono::milliseconds, you're storing it in std::chrono::system_clock::duration (the type of m_elapsed). Therefore, m_elapsed.count() returns the number of ticks in std::chrono::system_clock::duration's frequency, which is probably microseconds on your platform.

In other words, you're immediately undoing the cast to milliseconds by storing the result in something other than milliseconds.

std::chrono::duration<Rep,Period>::count , Class template std::chrono::duration represents a time interval. It consists of a count of ticks of type Rep and a tick period, where the tick returns the special duration value zero std::chrono::milliseconds, duration</*signed integer type of at least A std::chrono::duration literal representing microseconds Doesn't count return the count of std::chrono::milliseconds here? Why should I have to divide by 1000 here? Does .cout() return always in micorseconds or am I doing a mistake?

std::chrono::duration, std::chrono::duration::count. constexpr rep count() const;. Get count. Returns the std::chrono::milliseconds // std::chrono::duration_cast int main () { using  3 ms duration has 3 ticks 6000 us duration has 6000 ticks 3.5 30Hz duration has 3.5 ticks [] See als

I faced with similar problem and I resolved it by simple change from:

std::chrono::system_clock::duration              m_elapsed;


auto m_elapsed;

duration::count - C++ Reference, std::chrono::duration_cast. template <class ToDuration, class Rep, class Period> constexpr ToDuration duration_cast (const duration<Rep,Period>& dtn);. returns the count of ticks std::chrono::milliseconds: 1000000 microseconds 100000000 shakes 100 jiffies 0 minutes 0.82672 microfortnights 0.316887 nanocenturies.

duration_cast - C++ Reference, constexpr rep count() const;. Returns the number of ticks for this duration. #​include <chrono> #include <iostream> int main() { std::chrono::milliseconds ms{3​}; // 3 milliseconds // 6000 microseconds constructed from 3 milliseconds  If you need smaller ticks than that (like what's available for std::chrono::milliseconds or std::chrono::nanoseconds, you need to explicitly specify those smaller tick sizes, like typedef std::duration<unsigned long long, std::ratio<1, 1'000'000'000>> my_duration to get a type nearly identical to std::chrono::nanoseconds.

std::chrono::duration::count, using namespace std;. // main function to measure elapsed time of a C++ chrono::duration_cast<chrono::nanoseconds>(end - start).count(). << " ns" << endl;. Return value. d converted to a duration of type ToDuration. [] NoteThe function does not participate in overload resolution unless ToDuration is an instance of std::chrono::duration.

Measure elapsed time of a C++ program using chrono library, What Happens if I Assign m3 + us3 to minutes Instead of microseconds ? tick count within a duration , there is a member count() which simply returns the stored holds the number of milliseconds from start to end. std::cout << ms.count​() << "ms\n"; void f(boost::chrono::nanoseconds d) { // d.count() == 3000 when passed  In this post, we will discuss how to measure elapsed time of a C++ program in seconds, milliseconds, microseconds and nanoseconds using chrono library. Since C++11, the best way to measure elapsed time in C++ is by using the chrono library which deal with time.

  • Since you are accessing m_start from different threads, you should look into std::mutex.
  • Here is a video <chrono> tutorial:
  • @Angew. Yes, I have the mutexes in the actual code which is much more complex than this example. I have just created out an example code to discuss and understand the problem without involving too many details into it which worked I think. Thanks a lot for the useful answer