I suppose you can keep an object of type time_point if you like. But it is not required to get the time as you can call that method directly. It already keeps system time for you, or more precisely, the now method fetches the current value from the OS or hardware or whatever it uses.
#include<iostream>
#include<chrono>
#include<limits>
class Timer {
public:
Timer() {
m_start_time = std::chrono::high_resolution_clock::now();
}
~Timer() {
stop();
}
void stop() {
auto end_time = std::chrono::high_resolution_clock::now();
auto start = std::chrono::time_point_cast<std::chrono::microseconds>(m_start_time).time_since_epoch().count();
auto end = std::chrono::time_point_cast<std::chrono::microseconds>(end_time).time_since_epoch().count();
auto duration = end - start;
double ms = duration * 0.001;
std::cout << duration << "us (" << ms << "ms)\n";
}
private:
std::chrono::time_point<std::chrono::high_resolution_clock> m_start_time;
};
int main () {
{
Timer test; // Just create and object and you can find out long that
// takes to run on your machine.
//This below is nonsense just to give the computer something to do.
std::string a;
for(int i = 0; i != 1000; ++i)
a += "a";
std::cout << a.size() << '\t' << a.substr(0,500) << '\n';
}
return 0;
}
what's the formula to convert the type_point to a ledgiable time? could someone show an example?
you need a bunch of casts to make it printable. The way c++ does dates and times is a mix of things from C/ 1980s clunk and modern overengineered designs. The C clunk is simple and it works, but its also not objects which you asked for. The C++ stuff has objects, too many of them, but ironically to get a decent print of them you have to ... cast them ... back into the C stuff! It is an unholy mess, IMHO. There are likely 3rd party tools that can do this cleanly for you as OOP, if you don't want to build your own.
In short, you would think there was an object that would have month/day/year/hours/min/sec/decimalsubsecond fields of some sort. You would be wrong, as far as I know, there isnt one. You can put everything you need to know for general purpose (eg all but high res performance stuff) use into one 64 bit integer and methods to extract the fields, but that was too simple I guess. (I think that is what the C style stuff does, but again, they did not wrap to objects).
Try this.
It's adapted from cppreference and can be run in a separate terminal instance or window while other thinks are being done.
If that's what you want then the next step is a class and consideration to the time display and display frequency.