简体   繁体   English

在cout中进行算术运算时,chrono duration_cast无效

[英]chrono duration_cast not working when doing arithmetic in cout

I do not understand the following behaviour 我不明白以下行为

unsigned long begin_time = \
    std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now().time_since_epoch()).count();

//some code here

std::cout << "time diff with arithmetic in cout: " << \
    std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now().time_since_epoch()).count() - begin_time << std::endl;

unsigned long time_diff = \
    std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now().time_since_epoch()).count() - begin_time;

std::cout << "time_diff: " << time_diff << std::endl;

Output: 输出:

time diff with arithmetic in cout: <very large number (definitely not milliseconds)>
time_diff: <smaller number (definitely milliseconds)>

Why does the duration_cast not work when I do arithmetic within the cout? 当我在cout中进行算术运算时,为什么duration_cast不起作用? I have used unsigned int and int for the time_diff variable, but I always get good output when I first do the arithmetic within the variable initialization or assignment. 我已经使用unsigned intint作为time_diff变量,但是当我在变量初始化或赋值中首次执行算术时,我总是得到良好的输出。

NOTE 注意

I am using Visual Studio 2013 (Community edition) 我正在使用Visual Studio 2013(社区版)

You are probably overflowing unsigned long (sizeof is 4): 你可能溢出unsigned long (sizeof是4):

unsigned long begin_time = \
    std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now().time_since_epoch()).count();

Recommended: 推荐的:

using namespace std::chrono;
auto begin_time = steady_clock::now();

//some code here

std::cout << "time diff with arithmetic in cout: " << 
    duration_cast<milliseconds>(steady_clock::now() - begin_time).count() << std::endl;

There is nothing wrong with duration_cast , the problem is that an unsigned long is not large enough to handle a time in milliseconds since epoch. duration_cast没有任何问题,问题是unsigned long不足以处理自纪元以来毫秒级的时间。 From ideone I get this output: 从ideone我得到这个输出:

Max value for `unsigned long`: 4294967295
Milliseconds since epoch:     15426527488

I get the number of milliseconds by directly outpouting: 我通过直接输出获得毫秒数:

std::cout << std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now().time_since_epoch()).count() << std::endl;

In your first output, you get a gigantic number because begin_time is cast to std::chrono::milliseconds::rep (the return type of .count() ) which is large enough to handle the time_since_epoch (guaranted by the standard), while in your second output, both value are truncated by the unsigned long and thus you get a (probably) correct result. 在你的第一个输出中,你得到一个巨大的数字,因为begin_timebegin_timestd::chrono::milliseconds::rep begin_time std::chrono::milliseconds::rep (返回类型为.count() ),它足以处理time_since_epoch (由标准保证),在第二个输出中,两个值都被unsigned long截断,因此得到(可能)正确的结果。

Note: There may be architecture where an unsigned long is enough to handle this but you should not rely on it and directly use the arithmetic operators provided for std::chrono::duration . 注意:可能存在一种体系结构,其中unsigned long足以处理此问题,但您不应该依赖它并直接使用为std::chrono::duration提供的算术运算符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM