Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::chrono::duration_cast

      From cppreference.com
      <cpp‎ |chrono‎ |duration
       
       
      Date and time library
       
      std::chrono::duration
       
      Defined in header<chrono>
      template<class ToDuration,class Rep,class Period>
      constexpr ToDuration duration_cast(conststd::chrono::duration<Rep, Period>& d);
      (since C++11)

      Converts astd::chrono::duration to a duration of different typeToDuration.

      The function only participate in overload resolution ifToDuration is a specialization ofstd::chrono::duration.

      Let

      • ToRep betypename ToDuration::rep,
      • ToPeriod betypename ToDuration::period,
      • CF bestd::ratio_divide<Period, ToPeriod>,
      • CR bestd::common_type<Rep, ToRep,std::intmax_t>::type,
      • cr_count bestatic_cast<CR>(d.count()),
      • cr_num bestatic_cast<CR>(CF::num), and
      • cr_den bestatic_cast<CR>(CF::den),

      the result is:

      CF::num
      1not1
      CF::den1ToDuration(static_cast<ToRep>
                     (d.count()))
      ToDuration(static_cast<ToRep>
                     (cr_count* cr_num))
      not1ToDuration(static_cast<ToRep>
                     (cr_count/ cr_den))
      ToDuration(static_cast<ToRep>
                     (cr_count* cr_num/ cr_den))

      Contents

      [edit]Parameters

      d - duration to convert

      [edit]Return value

      d converted to a duration of typeToDuration.

      [edit]Notes

      No implicit conversions are used. Multiplications and divisions are avoided where possible, if it is known at compile time that one or more parameters are1. Computations are done in the widest type available and converted, as if bystatic_cast, to the result type only when finished.

      Casting between integer durations where the source period is exactly divisible by the target period (e.g. hours to minutes) or between floating-point durations can be performed with ordinary casts or implicitly viastd::chrono::duration constructors, noduration_cast is needed.

      Casting from a floating-point duration to an integer duration issubject to undefined behavior when the floating-point value is NaN, infinity, or too large to be representable by the target's integer type. Otherwise, casting to an integer duration is subject to truncation as with anystatic_cast to an integer type.

      [edit]Example

      This example measures the execution time of a function.

      Run this code
      #include <chrono>#include <iostream>#include <ratio>#include <thread> void f(){std::this_thread::sleep_for(std::chrono::seconds(1));} int main(){constauto t1=std::chrono::high_resolution_clock::now();    f();constauto t2=std::chrono::high_resolution_clock::now(); // floating-point duration: no duration_cast neededconststd::chrono::duration<double,std::milli> fp_ms= t2- t1; // integral duration: requires duration_castconstauto int_ms= std::chrono::duration_cast<std::chrono::milliseconds>(t2- t1); // converting integral duration to integral duration of// shorter divisible time unit: no duration_cast neededconststd::chrono::duration<long,std::micro> int_usec= int_ms; std::cout<<"f() took "<< fp_ms<<", or "<< int_ms<<" (whole milliseconds), or "<< int_usec<<" (whole microseconds)\n";}

      Possible output:

      f() took 1000.14ms, or 1000ms (whole milliseconds), or 1000000us (whole microseconds)

      [edit]See also

      (C++11)
      a time interval
      (class template)[edit]
      converts a time point to another time point on the same clock, with a different duration
      (function template)[edit]
      converts a duration to another, rounding down
      (function template)[edit]
      converts a duration to another, rounding up
      (function template)[edit]
      converts a duration to another, rounding to nearest, ties to even
      (function template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/chrono/duration/duration_cast&oldid=175306"

      [8]ページ先頭

      ©2009-2026 Movatter.jp