c ++ chrono duration_cast到毫秒会以秒为单位 [英] c++ chrono duration_cast to milliseconds results in seconds
问题描述
我想有自纪元以来的毫秒数。一个流行的解决方案如下(此问题的解决方案之一获取时间自纪元以来的毫秒数,最好使用C ++ 11 chrono )
#include< iostream>
#include< chrono>
int main(){
auto millitime = std :: chrono :: duration_cast< std :: chrono :: milliseconds>
(std :: chrono :: system_clock :: now()。time_since_epoch())count();
std :: cout<<海上< std :: endl;
return 0;
}
通过调用 g ++
像 g ++ -std = c ++ 11 main.cpp -o timetest
导致输出
1372686001
秒。
这是glibc中的错误吗?在g ++?我的错误?
g ++(Debian 4.7.3-4)4.7.3
ldd(Debian EGLIBC 2.17-6 )2.17
更新:它使用g ++ 4.8时工作。那么这是一个gcc错误?
g ++ - 4.8(Debian 4.8.1-2)4.8.1
我想发生的是你正在使用GCC 4.7进行编译,但是运行-time链接器使用来自不同GCC版本的 libstdc ++。so
,并且为 std :: chrono:system_clock
。如果使用
LD_LIBRARY_PATH
或合适的链接器选项来确保您使用GCC 4.7 和编译,请使用 libstdc ++。 c $ c>,则结果应正确。
例如:
$ $ HOME / gcc / 4.7.1 / bin / g ++ -std = c ++ 11 t.cc
$ ./a.out
1372693222
$ LD_LIBRARY_PATH = $ HOME /gcc/4.7.1/lib64 ./a.out
1372693225128
因为对 system_clock :: now()
的调用位于 libstdc ++。so
库中,因此结果取决于哪个库在运行时使用,但从该值到毫秒
的 duration_cast
转换由内联模板完成,在编译时。如果编译时转换与运行时调用不一致,则结果不一致。
对于GCC 4.8.1, system_clock
实现改进为总是使用 clock_gettime
系统调用,如果它可用,这不是4.7的情况,所以它一贯使用高精度时钟无论GCC是如何配置的,这可能解释了为什么你看不到4.8.1的问题。
您应该始终确保正确的版本 libstdc ++。因此
在运行时使用。
I want to have the number of milliseconds since epoch. A popular solution looks like follows (one of the solutions of this question asked here Get time since epoch in milliseconds preferably using C++11 chrono )
#include <iostream>
#include <chrono>
int main() {
auto millitime = std::chrono::duration_cast<std::chrono::milliseconds>
(std::chrono::system_clock::now().time_since_epoch()).count();
std::cout << millitime << std::endl;
return 0;
}
compiling this with a call to g++
like g++ -std=c++11 main.cpp -o timetest
results in the output
1372686001
which is equal to the number of seconds since epoch!
Is this a bug in the glibc? in g++? my mistake?
g++ (Debian 4.7.3-4) 4.7.3
ldd (Debian EGLIBC 2.17-6) 2.17
Update: it works when using the g++ 4.8. So it is a gcc bug?!
g++-4.8 (Debian 4.8.1-2) 4.8.1
I think what's happening is that you are compiling with GCC 4.7 but the run-time linker is using the libstdc++.so
from a different GCC version, and they are configured with different precision for std::chrono:system_clock
. If you use LD_LIBRARY_PATH
or suitable linker options to ensure you compile with GCC 4.7 and use its libstdc++.so
then the results should be correct.
For example:
$ $HOME/gcc/4.7.1/bin/g++ -std=c++11 t.cc
$ ./a.out
1372693222
$ LD_LIBRARY_PATH=$HOME/gcc/4.7.1/lib64 ./a.out
1372693225128
The difference happens because the call to system_clock::now()
is in the libstdc++.so
library so the result depends on which library is used at run-time, but the duration_cast
conversion from that value to milliseconds
is done by inline templates which are instantiated at compile-time. If the compile-time conversion is not consistent with the run-time call then the results are inconsistent.
For GCC 4.8.1 the system_clock
implementation was improved to always use the clock_gettime
system call if it's available, which was not the case for 4.7, so it consistently uses the high-precision clock no matter how GCC was configured, which probably explains why you don't see the problem with 4.8.1.
You should always ensure the right version of libstdc++.so
is used at run-time.
这篇关于c ++ chrono duration_cast到毫秒会以秒为单位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!