chrono

Get time since epoch in milliseconds, preferably using C++11 chrono

狂风中的少年 提交于 2019-11-27 05:21:25
问题 All I want is to get the time since epoch in milliseconds and store it in an unsigned long. I found this related question. But honestly, this can't be the easiest way to perform such a simple task, is it? I am hoping for something much simpler, but can't find anything in the std::chrono reference. Any advice is most welcome. I don't necessarily have to use std::chrono , but I want it to be platform independent. 回答1: unsigned long milliseconds_since_epoch = std::chrono::system_clock::now()

C++ How do I convert a std::chrono::time_point to long and back

对着背影说爱祢 提交于 2019-11-27 05:05:58
问题 I need to convert std::chrono::time_point to and from a long type (integer 64 bits). I´m starting working with std::chrono ... Here is my code: int main () { std::chrono::time_point<std::chrono::system_clock> now = std::chrono::system_clock::now(); auto epoch = now.time_since_epoch(); auto value = std::chrono::duration_cast<std::chrono::milliseconds>(epoch); long duration = value.count(); std::chrono::duration<long> dur(duration); std::chrono::time_point<std::chrono::system_clock> dt(dur); if

std::chrono::clock, hardware clock and cycle count

为君一笑 提交于 2019-11-27 04:48:12
问题 std::chrono offer several clocks to measure times. At the same time, I guess the only way a cpu can evaluate time, is by counting cycles. Question 1: Does a cpu or a gpu has any other way to evaluate time than by counting cycles? If that is the case, because the way a computer count cycles will never be as precise as an atomic clock, it means that a "second" ( period = std::ratio<1> ) for a computer can be actually shorter or bigger than an actual second, causing differences in the long run

What is the rationale for renaming monotonic_clock to steady_clock in <chrono>?

风流意气都作罢 提交于 2019-11-27 04:41:06
问题 Why did the committee rename monotonic_clock to steady_clock? Vendors are providing monotonic_clock for backwards compatibility so I expect monotonic_clock will linger for a while. It just seems a bit early to deprecate something in C++0x. ;) Edit: The committe has the right and responsibility to rename components the best they can before release as was done in this case. I don't see the big benefit of the rename. 回答1: N3128 is the proposal that did so and includes the rationale: The

std::put_time implementation status in GCC?

风格不统一 提交于 2019-11-27 04:09:35
I was trying to compile this example program using GCC (tested versions 4.5.1, 4.6.3, 4.8.4): #include <iostream> #include <iomanip> #include <ctime> #include <chrono> using std::chrono::system_clock; int main() { system_clock::time_point now = system_clock::now(); std::time_t now_c = system_clock::to_time_t( now - std::chrono::hours(24)); std::cout << "One day ago, the time was " << std::put_time(std::localtime(&now_c), "%F %T") << '\n'; } But it tells me: prog.cpp: In function 'int main()': prog.cpp:14:18: error: 'put_time' is not a member of 'std' I thought, probably it's not implemented

Handling an update loop using C++ Chrono?

不羁的心 提交于 2019-11-27 02:56:55
问题 I'm definitely a bit lost with the new C++ chrono library. Here I have an update loop. It runs two operations: engine.Update() engine.Render() These are long operations, and it's hard to tell how long they are. Thus, we measure how long they took, then do some calculations and figure the best way to gradually call update before we call render. To do this, i'm using C++11's Chrono functionality. I chose it because it sounded like a good deal: More accurate, More platform dependent. I'm finding

Handling Julian dates in C++11/14

試著忘記壹切 提交于 2019-11-27 02:13:18
问题 What is the best/easiest way to deal with Julian dates in C++? I want to be able to convert between Julian dates and Gregorian dates. I have C++11 and C++14. Can the <chrono> library help with this problem? 回答1: To convert between a Julian date and std::chrono::system_clock::time_point the first thing one needs to do is find out the difference between the epochs. The system_clock has no official epoch, but the de facto standard epoch is 1970-01-01 00:00:00 UTC (Gregorian calendar). For

precise time measurement

最后都变了- 提交于 2019-11-26 23:58:31
问题 I'm using time.h in C++ to measure the timing of a function. clock_t t = clock(); someFunction(); printf("\nTime taken: %.4fs\n", (float)(clock() - t)/CLOCKS_PER_SEC); however, I'm always getting the time taken as 0.0000. clock() and t when printed separately, have the same value. I would like to know if there is way to measure the time precisely (maybe in the order of nanoseconds) in C++ . I'm using VS2010. 回答1: I usually use the QueryPerformanceCounter function. example: LARGE_INTEGER

How to get the precision of high_resolution_clock?

為{幸葍}努か 提交于 2019-11-26 23:01:06
问题 C++11 defines high_resolution_clock and it has the member types period and rep . But I can not figure out how I can get the precision of that clock. Or, if I may not get to the precision, can I somehow at least get a count in nanoseconds of the minimum representable time duration between ticks? probably using period ? #include <iostream> #include <chrono> void printPrec() { std::chrono::high_resolution_clock::rep x = 1; // this is not the correct way to initialize 'period': //high_resolution

How to get duration, as int milli's and float seconds from <chrono>?

*爱你&永不变心* 提交于 2019-11-26 22:32:19
问题 I'm trying to use chrono library for timers and durations. I want to be able to have a Duration frameStart; ( from app start ) and a Duration frameDelta; ( time between frames ) I need to be able to get the frameDelta duration as milliseconds and float seconds. How do you do this with the new c++11 <chrono> libraries? I've been working on it and googling ( information is sparse ). The code is heavily templated and requires special casts and things, I can't figure out how to use this library