C++ Chrono 32/64 位 + 发布/调试
C++ Chrono 32/64 bits + Release/Debug
我在使用 c++ Visual Studio 2015 时遇到问题
请考虑此版本:
#ifndef CHRONO_INFO_H
#define CHRONO_INFO_H
#include <iostream>
#include <sstream>
#include <iomanip>
#include <chrono>
class c_ChronoInfo
{
private:
std::chrono::steady_clock::time_point _start;
std::chrono::steady_clock::time_point _stop;
std::chrono::steady_clock::duration _totaltime;
std::chrono::steady_clock::duration _laptime;
bool _isPaused;
const std::chrono::steady_clock::duration _zeroDuration = std::chrono::steady_clock::duration::zero();
public:
c_ChronoInfo() :
_totaltime(_zeroDuration),
_isPaused(true)
{
}
~c_ChronoInfo() {}
void Start()
{
assert(_isPaused);
_isPaused = false;
_start = std::chrono::steady_clock::now();
}
void Stop()
{
assert(!_isPaused);
_stop = std::chrono::steady_clock::now();
_laptime = _stop - _start;
_totaltime += _laptime;
_isPaused = true;
}
private:
inline auto _totalChronosI() const
{
return _totaltime;
}
public:
inline double TotalMilliseconds() const { return std::chrono::duration_cast<std::chrono::duration<double, std::chrono::milliseconds::period>>(_totalChronosI()).count(); }
};
#endif // CHRONO_INFO_H
当我以 64 位和发布模式编译时,按预期输出非常短的时间,例如使用:
c_ChronoInfo test;
test.Start();
int x = 0;
for (long long i = 0; i <= 100; ++i)
{
x = i;
}
test.Stop();
std::cout << "test: " << test.TotalMilliseconds() << std::endl;
但是当我以 32 位或调试(32 或 64(编译时,我会得到负数或非常大的时间。
此外,_laptime变量在类中定义,但实际上并不需要。 但是,如果在方法中根据需要对其进行定义,即
std::chrono::steady_clock::duration _laptime = _stop - _start;
即使在 64 位/发布模式下,代码的行为也完全出乎意料。
谢谢
在
非静态和非constexpr中_zeroDuration
,并且在使用它初始化构造函数初始化列表中的_totaltime
时尚未初始化,因为它是在类的后面声明的。
因此,内存中恰好包含一个值,该值后来看起来像一个大的负浮点数。
要修复初始化顺序,您可以将 _zeroDuration
声明移动到 _totaltime
// first
const std::chrono::steady_clock::duration _zeroDuration = std::chrono::steady_clock::duration::zero();
...
// second
std::chrono::steady_clock::duration _laptime;
bool _isPaused;
...
c_ChronoInfo() :
_totaltime(_zeroDuration),
_isPaused(true)
或直接初始化_totaltime
,如下所示:
c_ChronoInfo() :
_totaltime(std::chrono::steady_clock::duration::zero()),
或_zeroDuration
设为constexpr
:
static constexpr std::chrono::steady_clock::duration _zeroDuration = std::chrono::steady_clock::duration::zero();
您最好初始化所有 'std::chrono::d uration' 类型的变量,因为它们具有默认的 constexpr 构造函数并且未初始化为零。
您不需要duration_cast
TotalMilliseconds
:
inline double TotalMilliseconds() const {
return std::chrono::duration<double, std::milli>(_totalChronosI()).count();
}
您的类存在字段初始化重新排序问题_zeroDuration
该问题总是在_totaltime
后初始化,因为它们是按这样的顺序声明的,因此在调用_totaltime(_zeroDuration)
_totaltime
之后仍然充满垃圾。要修复它,只需扔掉_zeroDuration
:
c_ChronoInfo() :
_totaltime(std::chrono::steady_clock::duration::zero())
相关文章:
- 为什么使用__LINE_的代码在发布模式下在MSVC下编译,而不是在调试模式下
- 小字符串优化(调试与发布模式)
- 发布版本中的 Qt 隐形组合框(但不在调试版本中)
- 代码适用于调试,但不适用于发布
- Visual Studio 发布模式阻止在调试模式下执行的代码.使用 WinHTTP 和多线程
- 自定义内存管理器在发布模式下工作正常,但在调试模式下则不然
- 当 2 个线程共享同一物理内核时,具有错误共享的易失性增量在发布中的运行速度比在调试中慢
- 在 CMake 中发布调试
- c++ 矢量提取方法在发布中返回 nullptr,但不返回调试
- 不同的输出调试与发布
- 在调试模式下,所有程序都运行良好,但在发布模式下,我在cudaMalloc操作中出错
- 为发布和调试生成C++ dll
- 为什么这个来自 Objective-C++ 的dynamic_cast在调试中成功但在发布时失败?
- Visual Studio C++ - 确定是在调试版本还是发布版本中
- 将C++函数从 DLL 导出为 C 时调试和发布之间的差异
- C++ Chrono 32/64 位 + 发布/调试
- Libboost后缀适合发布/调试项目设置
- 以下代码在 VS 2013 发布/调试中生成不同的结果
- C DLL和Python CTypes-发布/调试中的不同返回
- 构建发布/调试VS2010 c++