chrono

Why does the C++ standard require the `Clock::now` function to be `static`?

流过昼夜 提交于 2019-12-08 20:41:19
问题 With C++11, C++ has some timing facilities in the standard. One of these facilities is a standard interface for clocks, that basically allows getting the time at the call of the now function of the clock. All is well up until this point but I fail to see the reason for requiring now to be a static function. On a hosted system, the standard clocks might be implementable purely with system calls or by reading the processor counters etc. However, this limits the implementation of custom clocks

g++ vs intel/clang argument passing order?

♀尐吖头ヾ 提交于 2019-12-08 19:53:49
问题 Consider the following code (LWS): #include <iostream> #include <chrono> inline void test( const std::chrono::high_resolution_clock::time_point& first, const std::chrono::high_resolution_clock::time_point& second) { std::cout << first.time_since_epoch().count() << std::endl; std::cout << second.time_since_epoch().count() << std::endl; } int main(int argc, char* argv[]) { test(std::chrono::high_resolution_clock::now(), std::chrono::high_resolution_clock::now()); return 0; } You have to run it

Is behaviour well-defined when `sleep_until()` specifies a time point in the past?

送分小仙女□ 提交于 2019-12-08 16:49:01
问题 The C++11 standard talks about what should happen if the system clock is adjusted such that the time point passed to sleep_until() is now in the past - but I can't see anywhere that addresses the case when the specified time point is already in the past. Have I simply overlooked something, or is it really not specified - even as UB or implementation-defined? A similar question arises if sleep_for() is invoked with a negative duration. 回答1: Calculation of time until which to sleep and calling

How to get the real calendar microseconds time (epoch since 1970) in Mac OSX?

。_饼干妹妹 提交于 2019-12-08 07:37:54
问题 Kindly go through below Qn for the context: Why does clang++/g++ not giving correct microseconds output for chrono::high_resolution_clock::now() in Mac OSX? As already discussed in above thread, I intend to get microseconds time since 1970. Now using chrono::high_resolution_clock::now().time_since_epoch() works well in popular platforms except OSX & possibly iOS. In [our] Mac systems, the microseconds time is generated since the system restart & not since 1970. Is there any portable [or Mac

std::chrono default duration for time_since_epoch

a 夏天 提交于 2019-12-08 01:28:37
问题 If I have the following clock and use it to get a count of ticks since the clock's epoch, what does this count actually represent. std::chrono::high_resolution_clock::now().time_since_epoch().count(); For instance I just ran this and got 1389375799048790227 . What does this number mean? Is it nanoseconds, microseconds, etc? 回答1: The type of the duration is std::chrono::high_resolution_clock::duration . You can inspect a duration's tick period with: std::chrono::high_resolution_clock::duration

Inaccuracy in std::chrono::high_resolution_clock? [duplicate]

六月ゝ 毕业季﹏ 提交于 2019-12-07 06:30:31
问题 This question already has answers here : What are the uses of std::chrono::high_resolution_clock? (2 answers) Closed 2 years ago . So I was trying to use std::chrono::high_resolution_clock to time how long something takes to executes. I figured that you can just find the difference between the start time and end time... To check my approach works, I made the following program: #include <iostream> #include <chrono> #include <vector> void long_function(); int main() { std::chrono::high

How to format std::chrono durations?

落花浮王杯 提交于 2019-12-07 01:42:29
问题 Is there a convenient way to format std::chrono::duration to a specified format? std::chrono::high_resolution_clock::time_point now, then; then = std::chrono::high_resolution_clock::now(); // ... now = std::chrono::high_resolution_clock::now(); auto duration = now - then; // base in microseconds: auto timeInMicroSec = std::chrono::duration_cast<std::chrono::microseconds>(duration); How can I format timeInMicroSec like ss::ms::us ? 回答1: One can use something like: #include <iomanip> #include

std::chrono default duration for time_since_epoch

馋奶兔 提交于 2019-12-06 11:17:30
If I have the following clock and use it to get a count of ticks since the clock's epoch, what does this count actually represent. std::chrono::high_resolution_clock::now().time_since_epoch().count(); For instance I just ran this and got 1389375799048790227 . What does this number mean? Is it nanoseconds, microseconds, etc? The type of the duration is std::chrono::high_resolution_clock::duration . You can inspect a duration's tick period with: std::chrono::high_resolution_clock::duration::period::num and std::chrono::high_resolution_clock::duration::period::den . This is the numerator and

non conforming return value for std::chrono::duration::operator%() in Microsoft C++ 2012

时光怂恿深爱的人放手 提交于 2019-12-06 07:41:38
I'm in the process of porting some C++ code to Windows (from Linux/g++4.8.1) and I noticed that Microsoft's implementation of the duration's modulus operator is incorrect. The simple program #include <chrono> #include <iostream> using namespace std::chrono; int main(void) { std::cout << (milliseconds(1050)%seconds(1)).count() << std::endl; return 0; } when compiled with Microsoft Visual Studio 2012 gives the compilation error: error C2228: left of '.count' must have class/struct/union The standard ( http://en.cppreference.com/w/cpp/chrono/duration/operator_arith4 ) has the definition as

Using <chrono> as a timer in bare-metal microcontroller?

前提是你 提交于 2019-12-06 06:09:53
问题 Can chrono be used as a timer/counter in a bare-metal microcontroller (e.g. MSP432 running an RTOS)? Can the high_resolution_clock (and other APIs in chrono) be configured so that it increments based on the given microcontroller's actual timer tick/register? The Real-Time C++ book (section 16.5) seems to suggest this is possible, but I haven't found any examples of this being applied, especially within bare-metal microcontrollers. How could this be implemented? Would this be even recommended?