chrono

Std::chrono or boost::chrono support for CLOCK_MONOTONIC_COARSE

≡放荡痞女 提交于 2019-12-06 04:59:32
Running on Linux (uname says:) Linux 2.6.32-431.29.2.el6.x86_64 #1 SMP Sun Jul 27 15:55:46 EDT 2014 x86_64 x86_64 x86_64 GNU/Linux My tests show that clock_gettime calls with a clock id of CLOCK_MONOTONIC_COARSE are an order of magnitude faster than calls that use a clock id CLOCK_MONOTONIC. Here's a sample output from a test run which called clock_gettime one million times in a tight loop and measured the lapsed time in milliseconds: CLOCK_MONOTONIC lapse 795 CLOCK_MONOTONIC_COARSE lapse 27 This pleases me and makes the profiler results look better, however I was hoping that I could use std:

Limiting fps with std::chrono

心不动则不痛 提交于 2019-12-06 00:43:49
std::chrono::system_clock::time_point m_BeginFrame = std::chrono::system_clock::now(); std::chrono::system_clock::time_point m_EndFrame = std::chrono::system_clock::now(); std::chrono::nanoseconds m_WorkTime = std::chrono::nanoseconds::zero(); std::chrono::nanoseconds m_WaitTime = std::chrono::nanoseconds::zero(); auto invFpsLimit = std::chrono::nanoseconds(1e9 / fpsLimit()); // main loop while (!glfwWindowShouldClose(m_pScreen->glfwWindow())) { m_WaitTime = m_BeginFrame - m_EndFrame; m_EndFrame = std::chrono::system_clock::now(); m_WorkTime = m_EndFrame - m_BeginFrame; // if need sleep if (m

Convert double to time_t

筅森魡賤 提交于 2019-12-06 00:00:31
问题 I have a double containing seconds. I would like to convert this into a time_t . I can't find a standard function which accomplishes this. Do I have to fill out the time_t by hand? 回答1: The type of std::time_t is unspecified. Although not defined, this is almost always an integral value holding the number of seconds (not counting leap seconds) since 00:00, Jan 1 1970 UTC, corresponding to POSIX time. So, just a safe casting between them could be fine. Also be carefull about portability

Persisting std::chrono time_point instances

我只是一个虾纸丫 提交于 2019-12-05 12:48:45
问题 What is the correct way to persist std::chrono time_point instances and then read them back into another instance of the same type? typedef std::chrono::time_point<std::chrono::high_resolution_clock> time_point_t; time_point_t tp = std::chrono::high_resolution_clock::now(); serializer.write(tp); . . . time_point_t another_tp; serializer.read(another_tp); The calls to write/read, assume that the instance of type time_point_t, can be somehow converted to a byte representation, which can then be

Actual millis in a C++

做~自己de王妃 提交于 2019-12-05 10:33:34
Is it possible to get the actual millis since I-don't-know in a C++-programm like System.currentTimeMillis() in Java? I know time() , but I think it's not exactly enough to measure short times, is it? It's part of the language standard these days (some years now): See it Live On Coliru #include <chrono> #include <iostream> int main() { using namespace std::chrono; auto epoch = high_resolution_clock::from_time_t(0); // ... auto now = high_resolution_clock::now(); auto mseconds = duration_cast<milliseconds>(now - epoch).count(); std::cout << "millis: " << mseconds; } In C++ you also have clock()

why is std::chrono::duration based on seconds

限于喜欢 提交于 2019-12-05 07:04:51
I'm learning <chrono> library, and considering the std::chrono::duration class, is there any specific reason to base it on seconds? For example a variable to store seconds would be chrono::duration<int> two_seconds(2); and all other time spans require relating them to seconds, like chrono::duration<int, ratio<60>> two_minutes(2); chrono::duration<int, ratio<1, 1000>> two_milliseconds(2); chrono::duration<int, ratio<60 * 60 * 24>> two_days(2); Are there any reasons to base duration on seconds and not on minutes, hours, etc.? Seconds are chosen because it's the basic time unit in both the SI

How to format std::chrono durations?

我的未来我决定 提交于 2019-12-05 06:05:20
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 ? One can use something like: #include <iomanip> #include <sstream> //... auto c(timeInMicroSec.count()); std::ostringstream oss; oss << std::setfill('0') // set field

undefined reference to `boost::chrono::system_clock::now()' - Boost, and cpp-netlib

被刻印的时光 ゝ 提交于 2019-12-05 05:51:39
I come here to ask for a fix to a situation that has been frustrating me. A lot. First of all, I'm on Windows, I use MinGW as a compiler (C++). I've been having some problems with getting a program to work with the use of cpp-netlib and SSL (trying to POST to a https site). I believe everything is in order except this one error that keeps evading me. C:\boost_1_50_0\boost_1_50_0\stage\lib\libboost_thread-mgw46-mt-1_50.a(thread.o):thread.cpp|| undefined reference to 'boost::chrono::system_clock::now()' I'm sure that I've linked to chrono, as well as all the .a libs in BOOST_ROOT/stage/lib . I

Get POSIX epoch as system_clock::time_point

喜夏-厌秋 提交于 2019-12-05 02:37:32
I'm aware that the default value of a std::chrono::system_clock::time_point is the clock's epoch, but I can't find any mandate in the C++11 standard that system_clock 's epoch is the same as the POSIX epoch (1970-01-01T00:00:00Z). Is it safe to assume on Linux and Windows that this is the case? Or would it be smarter to use std::chrono::system_clock::from_time_t(0) ? The standard leaves the epoch of std::chrono::system_clock::time_point unspecified. There are three implementations of std::chrono::system_clock::time_point I am aware of: libc++ libstdc++ VS All three of these are thin wrappers

Chrono Timer Not Converting Seconds Properly

偶尔善良 提交于 2019-12-04 21:31:42
I am having an interesting, yet strange issue with my game timer. It seems like the milliseconds works just fine. However, when I try to apply the std::chrono::seconds cast I suddenly get 0.000000 when casting to a float. My timer is as follows: #include <iostream> #include <time.h> #include <chrono> class Timer { public: typedef std::chrono::high_resolution_clock Time; typedef std::chrono::milliseconds ms; //<--If changed to seconds, I get 0.00000 typedef std::chrono::duration<float> fsec; std::chrono::high_resolution_clock::time_point m_timestamp; float currentElapsed; Timer() { m_timestamp