c++使运行时间独立于平台
c++ get elapsed time platform independent
对于一个游戏,我想测量自上一帧以来经过的时间。
我用glutGet(GLUT_ELAPSED_TIME)
来做那件事。但是在包含glew之后,编译器再也找不到glutGet函数了(奇怪)。所以我需要一个替代方案。
到目前为止,我发现的大多数网站都建议在ctime中使用时钟,但该功能只测量程序的cpu时间,而不是实时时间!ctime中的时间函数仅精确到秒。我需要至少毫秒的精度。
我可以使用C++11。
我认为在C++11之前没有内置高分辨率时钟。如果你不能使用C++11,你必须用glut和glew修复你的错误,或者使用依赖于平台的定时器函数。
#include <chrono>
class Timer {
public:
Timer() {
reset();
}
void reset() {
m_timestamp = std::chrono::high_resolution_clock::now();
}
float diff() {
std::chrono::duration<float> fs = std::chrono::high_resolution_clock::now() - m_timestamp;
return fs.count();
}
private:
std::chrono::high_resolution_clock::time_point m_timestamp;
};
- Boost提供类似std::chrono的时钟:Boost::chrono
- 您应该考虑使用std::chrono::steady_clock(或boost等效物),而不是std::chrono::high_resolution_clock-或者至少确保std::strono::steady_clock::is_steady()==true-如果您想使用它来计算持续时间,因为非稳定时钟返回的时间甚至可能随着物理时间的推移而减少
相关文章:
- 如何创建独立于平台的宏来包装编译器扩展?
- 为什么C++哈希函数的返回类型是 std::size_t,而不是独立于平台的类型?
- 如何在Qt WebChannel中以独立于平台的方式在HTML/Javascript和C++之间进行通信?
- "std::this_thread::sleep_for()"是否有特定于平台的限制
- 独立于平台的方式捕获控制台/终端关闭事件
- 如何实现独立于平台的异步文件写入
- 在C++中组织特定于平台的代码的推荐方法是什么?
- C++:如何保存独立于平台的二进制文件
- 是依赖于C++平台的
- 独立于平台的OpenSSL库
- 包括具有一个头文件和独立源文件的特定于平台的库
- 如何在C++中获取特定于平台的路径分隔符
- 如何以尽可能独立于平台的方式从 c++ 调用 .NET
- 最易于使用、轻量级、独立于平台的图形库,用于C++
- 用于在 C++ 中管理特定于平台的代码的内联命名空间技术
- windows上的boost.asio错误代码依赖于平台
- 尽管严重依赖JVM,但Java是如何独立于平台的
- 使用独立于平台的C/C++对数据块进行加密
- XSD限制可以是特定于平台的吗
- Mac地址-C++-独立于平台