chrono

How can this code be constexpr? (std::chrono)

雨燕双飞 提交于 2019-12-01 16:56:50
In the standards paper P0092R1, Howard Hinnant wrote: template <class To, class Rep, class Period, class = enable_if_t<detail::is_duration<To>{}>> constexpr To floor(const duration<Rep, Period>& d) { To t = duration_cast<To>(d); if (t > d) --t; return t; } How can this code work? The problem is that operator-- on a std::chrono::duration is not a constexpr operation. It is defined as: duration& operator--(); And yet this code compiles, and gives the right answer at compile time: static_assert(floor<hours>(minutes{3}).count() == 0, "”); What's up with that? The answer is that not all operations

VS11 is steady_clock, steady?

本秂侑毒 提交于 2019-12-01 14:40:06
问题 I just noticed the following code in <chrono.h> , which doesn't make sense to me. struct system_clock { static const bool is_monotonic = false; // retained static const bool is_steady = false; }; class steady_clock : public system_clock { // wraps monotonic clock public: static const bool is_monotonic = true; // retained static const bool is_steady = true; }; typedef steady_clock monotonic_clock; // retained typedef system_clock high_resolution_clock; How can steady_clock be steady when it

Measure execution time of arbitrary functions with C++14 lambda

落爺英雄遲暮 提交于 2019-12-01 06:25:38
I have been excited by item 24 of Scott Meyer's book "Effective Modern C++". He mentions the possibility to write a C++14 lambda to record the time taken in an arbitrary function invocation. I am still in an early of learning C++14 features. My attempt (Main.cpp) looks like this for measuring the time of a member function call: #include <chrono> #include <iostream> auto measure = [](auto&& function, auto&&... parameters) -> decltype(function) { const std::chrono::steady_clock::time_point startTimePoint = std::chrono::steady_clock::now(); const auto returnValue = std::forward<decltype(function)

odd behaviour using chrono::high_resolution_clock::now()

你说的曾经没有我的故事 提交于 2019-12-01 03:43:34
I've been looking at various game timing loop methods e.g. Glenn Fiedler and DeWitter. I found critical areas difficult to understand due to my own C++ knowledge limitations. With this I set about trying to implement my own method....I thought a good way to try to understand something about these methods. [edit1: I'm using CodeBlocks IDE with minGW-w64 (x64-4.8.1-posix-seh-rev5) as the compiler] [edit2: code and output windows amended to include a 3rd timer, QueryPerformanceCounter] In trying to accomplish this I came across the following issue: Minimal code: #include <chrono> #include

What's the Difference Between floor and duration_cast?

为君一笑 提交于 2019-12-01 03:35:00
So in c++11 the Chrono Library provides, duration_cast : Computations are done in the widest type available and converted, as if by static_cast, to the result type only when finished And c++17 's floor : Returns the greatest duration t representable in ToDuration that is less or equal to d So for all x will the result of these 2 calls be equal: chrono::duration_cast<chrono::seconds>(x) chrono::floor<chrono::seconds>(x) As far as I can tell, same as the difference between static_cast and std::floor : Negatives are rounded down instead of truncated toward zero. #include <iostream> #include

If I sleep for 10 milliseconds. what do I need to increment by to get a second?

青春壹個敷衍的年華 提交于 2019-12-01 01:54:51
i.e. i'm using std::this_thread::sleep_for(std::chrono::milliseconds(10)); in a program loop. If I have a variable that gets incremented on this loop to show seconds elapsed, what do I need to increment by? i.e. float x = 0; for each step: x += 0.01 I've tried 0.1, 0.01, 0.001, but they all seem either too fast or too slow. I would recommend using absolute time points and wait_until() . Something like this: // steady_clock is more reliable than high_resolution_clock auto const start_time = std::chrono::steady_clock::now(); auto const wait_time = std::chrono::milliseconds{10}; auto next_time =

accurate sampling in c++

…衆ロ難τιáo~ 提交于 2019-12-01 00:30:01
I want to sample values I get from a gpio 4000 times per second, currently I do something like that: std::vector<int> sample_a_chunk(unsigned int rate, unsigned int block_size_in_seconds) { std::vector<std::int> data; constexpr unsigned int times = rate * block_size_in_seconds; constexpr unsigned int delay = 1000000 / rate; // microseconds for (int j=0; j<times; j++) { data.emplace_back(/* read the value from the gpio */); std::this_thread::sleep_for(std::chrono::microseconds(delay)); } return data; } yet according to the reference sleep_for is guaranteed to wait for at least the specified

If I sleep for 10 milliseconds. what do I need to increment by to get a second?

ε祈祈猫儿з 提交于 2019-11-30 22:09:24
问题 i.e. i'm using std::this_thread::sleep_for(std::chrono::milliseconds(10)); in a program loop. If I have a variable that gets incremented on this loop to show seconds elapsed, what do I need to increment by? i.e. float x = 0; for each step: x += 0.01 I've tried 0.1, 0.01, 0.001, but they all seem either too fast or too slow. 回答1: I would recommend using absolute time points and wait_until() . Something like this: // steady_clock is more reliable than high_resolution_clock auto const start_time

What is the C++11 equivalent to boost::date_time::not_a_date_time?

℡╲_俬逩灬. 提交于 2019-11-30 18:33:37
问题 I'm modifying an old project, and at the same time I'm updating several things to bring it up to C++11. I'd like to replace various uses of boost::date_time with the new functionality in std::chrono. But I cannot figure out what is the C++11 equivalent of boost::date_time::not_a_date_time. Isn't there an equivalent in C++11 to indicate that a time_point variable hasn't yet been assigned, or doesn't contain a valid timestamp? 回答1: Given the fact that it exists as part of a group bool is

Measuring time results in return values of 0 or 0.001

元气小坏坏 提交于 2019-11-30 15:07:47
I am trying to use chrono::steady_clock to measure fractional seconds elapsed between a block of code in my program. I have this block of code working in LiveWorkSpace ( http://liveworkspace.org/code/YT1I$9 ): #include <chrono> #include <iostream> #include <vector> int main() { auto start = std::chrono::steady_clock::now(); for (unsigned long long int i = 0; i < 10000; ++i) { std::vector<int> v(i, 1); } auto end = std::chrono::steady_clock::now(); auto difference = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count(); std::cout << "seconds since start: " << ((double