c++ - Getting a time difference in milliseconds -
i´m trying thought simple have looked everywhere , can´t figure out. i´m new c++ , have no understanding of templates , such.
i need function measures time program´s launch point in milliseconds, like:
class timecounter { private: long starttime; long currenttime; long timedifference; public: long gettime(); } timecounter::timecounter () { starttime = time.now(); } long timecounter::gettimepassed () { currenttime = time.now(); timedifference = timenow - timestart; return timedifference; }
i´ve tried clock() / clocks_per_seconds
result slower second.
can me out?
thank much!
i writing similar system delta time game engine.
using std::chrono
library, here's example:
#include <iostream> #include <chrono> #include <thread> class timer { // alias our types simplicity using clock = std::chrono::system_clock; using time_point_type = std::chrono::time_point < clock, std::chrono::milliseconds > ; public: // default constructor stores start time timer() { start = std::chrono::time_point_cast<std::chrono::milliseconds>(clock::now()); } // gets time elapsed construction. long /*milliseconds*/ gettimepassed() { // new time auto end = clock::now(); // return difference of times return (end - start).count(); } private: time_point_type start; }; int main() { timer t; std::this_thread::sleep_for(std::chrono::seconds(5)); std::cout << t.gettimepassed(); std::cin.get(); }
Comments
Post a Comment