chrono

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

本秂侑毒 提交于 2019-11-28 01:30:22
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 for time measurements between the computer clock and let's say GPS. Question 2: Is that correct? Some

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

拈花ヽ惹草 提交于 2019-11-27 23:37:50
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. N3128 is the proposal that did so and includes the rationale: The implementation of the timeout definition necessarily depends on a steady clock, one that cannot be adjusted. A

How to get the precision of high_resolution_clock?

左心房为你撑大大i 提交于 2019-11-27 21:11: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_clock::period y = 1; std::cout << "The smallest period is " << /* what to do with 'x' or 'y' here? */ <

C++ chrono system time in milliseconds, time operations

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-27 17:35:21
I've got a small problem caused by insufficient documentation of C++11. I'd like to obtain a time since epoch in milliseconds, or nanoseconds or seconds and then I will have to "cast" this value to another resolution. I can do it using gettimeofday() but it will be to easy, so I tried to achieve it using std::chrono. I tried: std::chrono::time_point<std::chrono::system_clock> now = std::chrono::system_clock::now(); But I have no idea what is a resolution of obtained in this way time_point, and I don't know how to get this time as a simple unsigned long long, and I haven't any conception how to

Outputting Date and Time in C++ using std::chrono

Deadly 提交于 2019-11-27 17:19:57
I have been upgrading some old code and have been trying to update to c++11 where possible. The following code is how I used to display the time and date in my program #include <iostream> #include <string> #include <stdio.h> #include <time.h> const std::string return_current_time_and_date() const { time_t now = time(0); struct tm tstruct; char buf[80]; tstruct = *localtime(&now); strftime(buf, sizeof(buf), "%Y-%m-%d %X", &tstruct); return buf; } I would like to output the current time and date in a similar format using std::chrono(or similar) but am unsure how to go about doing so. Any help

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

耗尽温柔 提交于 2019-11-27 17:16:44
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 correctly. Is this what you're looking for? #include <chrono> #include <iostream> int main() { typedef

What are the uses of std::chrono::high_resolution_clock?

本秂侑毒 提交于 2019-11-27 12:24:21
问题 At first I thought it can be used for performance measurements. But it is said that std::chrono::high_resolution_clock may be not steady ( is_steady may be false ). It is also said that std::chrono::high_resolution_clock may even be an alias of std::chrono::system_clock which is generally not steady. So I can't measure time intervals with this type of clock because at any moment the clock may be adjusted and my measurements will be wrong. At the same time I can't convert time points of std:

How do you print a C++11 time_point?

最后都变了- 提交于 2019-11-27 10:15:16
问题 I've created a time point, but I have been struggling to print it to the terminal. #include <iostream> #include <chrono> int main(){ //set time_point to current time std::chrono::time_point<std::chrono::system_clock,std::chrono::nanoseconds> time_point; time_point = std::chrono::system_clock::now(); //print the time //... return 0; } The only documentation I can find that prints a time_point is found here: http://en.cppreference.com/w/cpp/chrono/time_point however, I'm not even able to create

c++ chrono duration_cast to milliseconds results in seconds

☆樱花仙子☆ 提交于 2019-11-27 07:00:53
问题 I want to have the number of milliseconds since epoch. A popular solution looks like follows (one of the solutions of this question asked here Get time since epoch in milliseconds, preferably using C++11 chrono ) #include <iostream> #include <chrono> int main() { auto millitime = std::chrono::duration_cast<std::chrono::milliseconds> (std::chrono::system_clock::now().time_since_epoch()).count(); std::cout << millitime << std::endl; return 0; } compiling this with a call to g++ like g++ -std=c+

How to convert std::chrono::time_point to string

时光毁灭记忆、已成空白 提交于 2019-11-27 06:44:11
问题 How to convert std::chrono::time_point to string? For example: "201601161125" . 回答1: The most flexible way to do so is to convert it to struct tm and then use strftime (it's like sprintf for time). Something like: std::chrono::system_clock::time_point now = std::chrono::system_clock::now(); std::time_t now_c = std::chrono::system_clock::to_time_t(now); std::tm now_tm = *std::localtime(&now_c); /// now you can format the string as you like with `strftime` Look up the documentation for strftime