c++ 计时的默认时间单位是什么?
What is the default unit of time of c++'s chrono?
我使用这段代码根据这个答案来测量程序的运行时间
auto start = std::chrono::system_clock::now();
/* do some work */
auto end = std::chrono::system_clock::now();
auto elapsed = end - start;
std::cout << elapsed.count() << 'n';
我只需要测量运行时间来寻找趋势,但我有点好奇那是什么单位
您可以轻松地自己检查,使用以下代码:
using Clock = std::chrono::system_clock;
using Duration = Clock::duration;
std::cout << Duration::period::num << " , " << Duration::period::den << 'n';
在我的系统上它打印:
1 , 1000000000
即以纳秒为单位的周期(即10&减去;9s)。
标准未指定
20.12.7.1 Class system_clock [time.clock.system]
system_clock类的对象表示来自全系统实时时钟的挂钟时间。
类system_clock {
公众:
typedef ratio<</strong>;
typedef空间:duration<</strong>;
time_point;
静态constexpr bool is_steady =未指定;
静态time_point now() noexcept;
//映射到C API
静态time_t to_time_t (const time_point&t) noexcept;
静态time_point from_time_t(time_t) noexcept;
}; typepedef unspecified system_clock::rep;
就我个人而言,我认为将结果显式地转换为您想要的特定duration
总是一个好主意。
相关文章:
- 为不同配置设置MSVC_RUNTIME_LIBRARY的正确方法是什么
- C++避免重复声明的语法是什么
- 在C++中,将大的无符号浮点数四舍五入为整数的最佳方法是什么
- 实现无开销push_back的最佳方法是什么
- C++从另一个类访问公共静态向量的正确方法是什么
- "throw expression code" 1e7 >返回 d 是什么?投掷标准::overflow_error( "too big" ) : d;意味 着?
- C++中名称篡改的目的是什么
- 在 c++ 中拥有一组结构的正确方法是什么?
- 这个指针和内存代码打印是什么?我不知道是打印垃圾还是如何打印我需要的值
- 是什么阻止DOMTimerCoordinator::NextID进入无休止的循环
- 派生类销毁的最佳实践是什么
- 这个语法std::class<>{}(arg1, arg2) 在C++中是什么意思?
- 通过JNI传递数据数组的最快方法是什么
- "using namespace std;"在C++的作用是什么?
- 在两台机器之间进行时间戳的最佳c++chrono函数是什么
- 文件系统:复制功能的速度秘诀是什么
- 对话框 MFC 资源度量的单位是什么
- 梯度幅度的单位和极限是什么
- 解码像 FAST 这样的数据协议的快速方法是什么,其中数据以字节为单位编码,并将位作为存在标志
- c++ 计时的默认时间单位是什么?