0

I am trying to use a function (the doAction() function in the example below) after a specific amount of time passes. In the following example, I have to use the function 10 times, first being 1 millisecond after having entered the 'while loop', second being 3 seconds after having entered the 'while loop', and so on.

Simply put, I need a method to measure how much time passed from the beginning of the 'while loop' to any given moment so that I can apply the doAction() function at specific desired moments.

QUESTION: What method can I use to track how much time has passed between an initial moment and any other moment afterwards? Edited: I am using windows 7

Currently I am using the following setup with the GetSystemTime() function which is working, though has some issues:

int countVar = 0;
int timeList[10] = {1, 3, 6, 7, 9, 11, 14, 15, 17, 19};
    /*values representing milliseconds after initiating the while loop*/

SYSTEMTIME timeBuffer;
GetSystemTime(&timeBuffer);
int initialTime = timeBuffer.wMilliseconds + (timeBuffer.wSecond*1000) + (timeBuffer.wMinute*60000);
int currentTime;

while (true)
{
   GetSystemTime(&timeBuffer);
   currentTime = timeBuffer.wMilliseconds + (timeBuffer.wSecond*1000) + (timeBuffer.wMinute*60000) - initialTime;

   if (currentTime >= timeList(countVar)
   {
      doAction();
      if (countVar!=9)
      countVar++;
      else
      break;

   }

}

the doAction function is just a really fast function which wouldn't have a significant impact on the time.

void doAction()  /*this is the action done which is completed in 
                 microseconds hence would not have a significant
                 delay in time*/
{
   ....
}

The problems this setup has however is as follows:

1) if the system time was 59 minutes and 59 seconds, the next system time I would look for would be over 59 minutes and 59 seconds, which I would not be able to obtain unless I also included wHours into the calculation, and the same can be said for days and weeks, etc.

2) I would prefer to use a function that when used, starts counting down milliseconds, and when reusing that function would tell me how many milliseconds have been used since the countdown started. However I haven't found any function like this available in msdn as of yet.

If anyone could point me to the direction of a better function to use than GetSystemTime() to measure how much time passes after a certain moment, that would be awesome!

Edited: I am using Windows 7

CodeBlocks
  • 233
  • 3
  • 5
  • 14
  • even though it's a interesting question, it should contain either windows/linux/any other OS tag too.. – basav Jan 24 '15 at 14:29
  • Is the current time really relevant? Wouldn't a stopwatch-like behavior be sufficient as well? – Caramiriel Jan 24 '15 at 14:30
  • In Linux, you have monotonic timers which would give you an accurate time interval...But not sure about Windows. – basav Jan 24 '15 at 14:33
  • @Caramiriel A stopwatch type of behavior would be perfect, do you know any type of functions for Windows that work similarly? – CodeBlocks Jan 24 '15 at 15:01
  • [`QueryPerformanceCounter`](http://stackoverflow.com/questions/1739259/how-to-use-queryperformancecounter) – Nazar554 Jan 24 '15 at 15:03
  • There is a world of difference between correct and simple. What you have now is definitely not correct. You are burning 100% core, the thread scheduler is going to put you in the dog house for 45 msec after you burned through your quantum. Simplest way to get something correctish is to call timeBeginPeriod(1) to jack up the interrupt rate, now you use Sleep() to delay. – Hans Passant Jan 24 '15 at 15:19
  • @HansPassant I will look into those functions now, thanks Hans – CodeBlocks Jan 24 '15 at 15:42
  • @Nazar554 Thank you for that suggestion! I am loving the QueryPerformanceCounter function. A question I couldn't find an answer to is when in fact the number you get from the QueryPerformanceCounter goes back to being zero. Does this have to do with when the computer restarts? Is there a function that can set this value to zero? – CodeBlocks Jan 24 '15 at 20:05

1 Answers1

0

If you are using C++11 you could:

include chrono

std::chrono::system_clock::time_point starts;

starts = std::chrono::system_clock::now();

Do your stuff here...

auto ends = std::chrono::system_clock::now();

auto elapsed = std::chrono::duration_cast(ends - starts);

This will give you the elapsed time in seconds.

Moo
  • 23
  • 4
  • could this work for milliseconds? I need the time to be accurate to at least 1 millisecond for my specific purpose. – CodeBlocks Jan 24 '15 at 15:44
  • Yes, you'll get all the information you need here: http://en.cppreference.com/w/cpp/chrono/duration – Moo Jan 24 '15 at 17:25