java nanoTime 与 C++ 纳秒
java nanoTime vs C++ nanoseconds
我想用两种语言打印纳米时间并比较值。
JAVA代码
long nano_startTime = System.nanoTime();
System.out.println(nano_startTime);
C++代码
system_clock::time_point begin = system_clock::now();
auto since_epoch = begin.time_since_epoch(); // get the duration since epoch
std::cout << std::to_string(duration_cast<nanoseconds>(since_epoch).count()) << std::endl;
我希望结果相同,但结果不同...!
结果
JAVA: 4459739378141
C++ 1584649009920663623
顺便说一句:如果我使用毫秒,结果是相同的。但我需要更精确的时间数据,不幸的是 java.
中没有微秒
有人可以帮助我吗?
谢谢
阅读 the Javadoc of System.nanoTime()
(强调已添加):
This method can only be used to measure elapsed time and is not related to any other notion of system or wall-clock time. The value returned represents nanoseconds since some fixed but arbitrary origin time (perhaps in the future, so values may be negative). The same origin is used by all invocations of this method in an instance of a Java virtual machine; other virtual machine instances are likely to use a different origin.
另一方面,在您的 C++ 代码中,since_epoch
是自纪元以来的时间(假设该方法没有随意命名;您也可以查看其文档)。
和System.currentTimeMillis()
returns:
the difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC.
所以 那 returns 自纪元以来的时间。
文档是开始了解方法行为的好地方。
我想用两种语言打印纳米时间并比较值。
JAVA代码
long nano_startTime = System.nanoTime();
System.out.println(nano_startTime);
C++代码
system_clock::time_point begin = system_clock::now();
auto since_epoch = begin.time_since_epoch(); // get the duration since epoch
std::cout << std::to_string(duration_cast<nanoseconds>(since_epoch).count()) << std::endl;
我希望结果相同,但结果不同...!
结果
JAVA: 4459739378141
C++ 1584649009920663623
顺便说一句:如果我使用毫秒,结果是相同的。但我需要更精确的时间数据,不幸的是 java.
中没有微秒有人可以帮助我吗? 谢谢
阅读 the Javadoc of System.nanoTime()
(强调已添加):
This method can only be used to measure elapsed time and is not related to any other notion of system or wall-clock time. The value returned represents nanoseconds since some fixed but arbitrary origin time (perhaps in the future, so values may be negative). The same origin is used by all invocations of this method in an instance of a Java virtual machine; other virtual machine instances are likely to use a different origin.
另一方面,在您的 C++ 代码中,since_epoch
是自纪元以来的时间(假设该方法没有随意命名;您也可以查看其文档)。
和System.currentTimeMillis()
returns:
the difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC.
所以 那 returns 自纪元以来的时间。
文档是开始了解方法行为的好地方。