0

I am working on open CV.The code is written in c++.I want to find out the time taken by the program to execute.its just a basic question.

kadu
  • 493
  • 3
  • 8
  • 19

2 Answers2

2

There are lots of ways to do that (see Easily measure elapsed time). A simple approach is the following:

#include <time>

void main()
{
    clock_t start_time = clock();
    // Do something
    clock_t end_time = clock();
    float time_in_seconds = (end_time-start_time)/(float)CLOCKS_PER_SEC;
}
Community
  • 1
  • 1
BConic
  • 8,750
  • 2
  • 29
  • 55
1

You have several options:

  • profile your program, that means run it in a special setting which will modify and/or monitor your code with timing probe and report statistics about how much time is spent in functions / lines of codes. Since you're using Visual Studio, depending on your version you could try its integrated profiler or try something like Intel VTune or AMD CodeAnalyst

  • manually add code to take time measurements at specific points in your code. In C++ and windows, an easy and accurate way of doing that is QueryPerformanceCounter from <windows.h>. It's more accurate than clock and less accurate than CPU's performance counters, but these are harder to exploit in a multi-core system.

Example for second option:

// init
#include <Windows.h>
LARGE_INTEGER freq;
QueryPerformanceFrequency(&freq);
double milliseconds = 1000.0 / freq.QuadPart;

// timed execution
LARGE_INTEGER start, stop;
QueryPerformanceCounter(&start);
call_your_function();
QueryPerformanceCounter(&stop);
double time_in_ms = (stop.QuadPart - start.QuadPart) * milliseconds;
Antoine
  • 13,494
  • 6
  • 40
  • 52