-2

I have an assignment that asks me to measure the exact run time of a couple of programs. I am not sure how to do this, but I think it has something to do with time_start(); as I've seen this on the internet. What does time_start(); mean, and how do I use it to measure run time? I'm using Windows 7, and Dev C++ Compiler

Thank you

C_Intermediate_Learner
  • 1,800
  • 3
  • 18
  • 22
  • 2
    Presumably whoever set the assignment gave you this information... – Carl Norum Jul 19 '13 at 05:40
  • Duplicate: http://stackoverflow.com/questions/5248915/execution-time-of-c-program Also, Google and StackOverflow are your friends. ;) – jrd1 Jul 19 '13 at 05:45

1 Answers1

2

"Precise" time measurement requires to use operating system specific functions.

Under Windows, you proably want to use the following function: (MSDN)

BOOL WINAPI QueryPerformanceCounter(_Out_  LARGE_INTEGER *lpPerformanceCount);

This function gives you a high-resolution counter of ticks since your application started. The interval between two ticks depends on your processor and might be retrieved using the following function: (MSDN)

BOOL WINAPI QueryPerformanceFrequency(_Out_  LARGE_INTEGER *lpFrequency);

So here is a snippet of code to get a precise value of the current time in seconds:

double GetCurTime()
{
    LARGE_INTEGER CounterFreq;
    QueryPerformanceFrequency(&CounterFreq);

    LARGE_INTEGER Counter;
    QueryPerformanceCounter(&Counter);
    return (double)Counter.QuadPart / (double)CounterFreq.QuadPart;
}

So to make a time measurement, call GetCurTime() at the beginning and call it again at the end, and take the difference between the two values.

Benlitz
  • 1,952
  • 1
  • 17
  • 30
  • Also note that you could retrieve the `PerformanceFrequency` at the beginning of your program and store the value, since it never changes during the execution. So you can reduce this function to a single system call instead of two. – Benlitz Jul 19 '13 at 08:18