Question

Does anyone know how to measure the FPS or execution time for C++ and for MATLAB? I'm looking for a correct way to measure that. I want to make a simple program (like edge detection in MATLAB or using OpenCV with C++) and want to compare the resultant speed of processing.

Some algorithms compute the time in milliseconds and others in microseconds.

I've found several ways to do it in C++, like using time_t start and time_t end (Processor time by clock) and calendar time. For MATLAB they use tic and toc to compute time.

Also, for C++ I have also find this method: https://www.dropbox.com/s/nb33hc3wumcpxb2/Timer.PDF

I want to find the same method for computing time that follows the exact procedure in both C++ and MATLAB.

Was it helpful?

Solution

When you say "same method", there is really only one way to do that which is accessing the system clock. There is more than one method to do this, but they ultimately rely on the same thing. Also, the paper you referred us to relies on using Windows and Visual C and so this would not be applicable to Mac OS or Linux. As such, I'm going to try and give you platform independent methods for both MATLAB and C++. In either platform, this will give you the execution time as best as it can.

MATLAB

You can use the tic and toc commands. You start with the tic command, then do your processing, then you finish with t = toc. t will thus contain how much time has elapsed in between tic and toc.

Here is a quick example:

tic;
for i = 1 : 100000
    M = rand(100,100); %// Generate a 100 x 100 uniformly random matrix
t = toc;
fprintf('The amount of time that has elapsed is %f seconds\n', t);

If you want to compute FPS, simply measure how long it takes to execute the algorithm for one image/frame, then take the reciprocal. This time would be seconds/frame and so to compute FPS, take the reciprocal.

C++

You can use the ctime library, and use the time(NULL) call. This will return the time in seconds since Epoch (January 1, 1970). Like the MATLAB example, here is a C++ example:

#include <ctime>

void func() {
  using namespace std;

  // Begin 
  int start = time(NULL);

  // Put in processing code here

  // End - time in seconds
  int finish = time(NULL);

  // Measure time elapsed
  int time_elapsed = finish - start;
}

To compute FPS, just follow what I did for the MATLAB example.

Another way using C++ (but with fair warning)

If you want microsecond precision to measure time in C++, you can use the clock() function. You can do something like this:

#include <ctime>

void func() {
  using namespace std;

  // Begin 
  clock_t start = clock();

  // Put in processing code here

  // End - time in microseconds
  clock_t finish = clock();

  // Measure time elapsed
  double time_elapsed = double(finish - start) / CLOCKS_PER_SEC;
}

However, the clock() function will measure CPU time and not the actual time elapsed, so you will need to be careful here if you decide to do this. Check this posting out for more details: Easily measure elapsed time

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top