C++ 如何从 <chrono> 获取持续时间,如 int 毫秒和浮点秒数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14391327/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 18:18:45  来源:igfitidea点击:

How to get duration, as int milli's and float seconds from <chrono>?

c++c++11timerchrono

提问by EddieV223

I'm trying to use chrono library for timers and durations.

我正在尝试将 chrono 库用于计时器和持续时间。

I want to be able to have a Duration frameStart;( from app start ) and a Duration frameDelta;( time between frames )

我希望能够有一个Duration frameStart;(从应用程序开始)和一个Duration frameDelta;(帧之间的时间)

I need to be able to get the frameDeltaduration as milliseconds and float seconds.

我需要能够以frameDelta毫秒和浮动秒为单位获得持续时间。

How do you do this with the new c++11 <chrono>libraries? I've been working on it and googling ( information is sparse ). The code is heavily templated and requires special casts and things, I can't figure out how to use this library correctly.

你如何使用新的 c++11<chrono>库来做到这一点?我一直在研究它并使用谷歌搜索(信息很少)。代码是大量模板化的,需要特殊的演员表和东西,我不知道如何正确使用这个库。

回答by Howard Hinnant

Is this what you're looking for?

这是你要找的吗?

#include <chrono>
#include <iostream>

int main()
{
    typedef std::chrono::high_resolution_clock Time;
    typedef std::chrono::milliseconds ms;
    typedef std::chrono::duration<float> fsec;
    auto t0 = Time::now();
    auto t1 = Time::now();
    fsec fs = t1 - t0;
    ms d = std::chrono::duration_cast<ms>(fs);
    std::cout << fs.count() << "s\n";
    std::cout << d.count() << "ms\n";
}

which for me prints out:

这对我来说打印出来:

6.5e-08s
0ms

回答by Jonathan Wakely

I don't know what "milliseconds and float seconds" means, but this should give you an idea:

我不知道“毫秒和浮点秒”是什么意思,但这应该给你一个想法:

#include <chrono>
#include <thread>
#include <iostream>

int main()
{
  auto then = std::chrono::system_clock::now();
  std::this_thread::sleep_for(std::chrono::seconds(1));
  auto now = std::chrono::system_clock::now();
  auto dur = now - then;
  typedef std::chrono::duration<float> float_seconds;
  auto secs = std::chrono::duration_cast<float_seconds>(dur);
  std::cout << secs.count() << '\n';
}

回答by Billy The Kid

Taking a guess at what it is you're asking for. I'm assuming by millisecond frame timer you're looking for something that acts like the following,

猜一猜你要的是什么。我假设通过毫秒帧计时器您正在寻找类似于以下内容的东西,

double mticks()
{
    struct timeval tv;
    gettimeofday(&tv, 0);
    return (double) tv.tv_usec / 1000 + tv.tv_sec * 1000;
}

but uses std::chronoinstead,

而是使用std::chrono

double mticks()
{
    typedef std::chrono::high_resolution_clock clock;
    typedef std::chrono::duration<float, std::milli> duration;

    static clock::time_point start = clock::now();
    duration elapsed = clock::now() - start;
    return elapsed.count();
}

Hope this helps.

希望这可以帮助。

回答by Chris Drew

In AAA styleusing the explicitly typed initializer idiom:

AAA 风格中,使用显式类型化的初始化习惯用法

#include <chrono>
#include <iostream>

int main(){
  auto start = std::chrono::high_resolution_clock::now();
  // Code to time here...
  auto end = std::chrono::high_resolution_clock::now();

  auto dur = end - start;
  auto i_millis = std::chrono::duration_cast<std::chrono::milliseconds>(dur);
  auto f_secs = std::chrono::duration_cast<std::chrono::duration<float>>(dur);
  std::cout << i_millis.count() << '\n';
  std::cout << f_secs.count() << '\n';
}