windows - C++: keeping track of elapsed time -


i'm looking way able know how time it's been since program started, @ given time. sort of timer keep running while main code doing else, , can called @ time.

the context opengl application on windows, , knowing keyboard keys being pressed (using glutkeyboardfunc), i'd know when each key pressed. of info written xml file later used replay user did. (sort of replay functionality in car racing game, more simple).

c++ 11:

#include <iostream> #include <chrono>  auto start = std::chrono::system_clock::now(); auto end = std::chrono::system_clock::now(); std::chrono::duration<double> elapsed_seconds = end - start; std::cout << "elapsed time: " << elapsed_seconds.count() << "s\n"; 

code taken en.cppreference.com , simplified.

old answer:
gettickcount() in windows.h returns ticks(miliseconds) elapsed. when app starts, call function , store value, whenever need know elapsed time since program start, call method again , subtract value start value.

int start = gettickcount(); // @ program start  int elapsed = gettickcount() - start; // elapsed time since start of program 

Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -