0

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.

michel-slm
  • 9,438
  • 3
  • 32
  • 31
user3617694
  • 143
  • 1
  • 1
  • 8

1 Answers1

0

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

Community
  • 1
  • 1
rayryeng
  • 102,964
  • 22
  • 184
  • 193
  • thank you for help me , please show this method in this paper https://www.dropbox.com/s/k0zv8pck7ydbakz/1_7-PDF_thesis_2.pdf – user3617694 May 11 '14 at 18:41
  • if i want to compute fps , https://www.dropbox.com/s/x90bd2afm4zd0ux/fps.txt is that correct or there are the method best – user3617694 May 11 '14 at 18:49
  • In that paper, one of the methods is pretty much what I talked about here with `clock()`. There is another method using `gettimeofday()` which is also in the paper, but this is dangerous. This method can result in incorrect timings if there are processes on your system that change the timer and it can jump forward and backward and time, so I wouldn't rely on this function. That text file with computing FPS, you just have to do 1 divided by `time_elapsed`. No need to use a counter. Just place whatever operations **you want timed** in between the two `clock()` statements. – rayryeng May 11 '14 at 20:26
  • If i want microsecond or millisec precision to measure actual time elapsed the first method in secound and method for windows c++ – user3617694 May 12 '14 at 16:23
  • I'm sorry that sentence doesn't make any sense. – rayryeng May 12 '14 at 16:24
  • ok, sorry am not good in english, by using time() get time in seconds when using clock() get time in microsecond but this CPU time not the actual time (like time()) if i want get time in millisec or microsecond actual time for windows ?? – user3617694 May 12 '14 at 16:58
  • this function gettimeofday (Calendar time) mention in this paper https://www.dropbox.com/s/k0zv8pck7ydbakz/1_7-PDF_thesis_2.pdf this function for linux and not fro windows i search and found alternative to it ,compute time in milli and microseconds https://www.dropbox.com/s/ofo99b166l7e2gf/gettimeofday.txt – user3617694 May 12 '14 at 17:50
  • please help me , iwant any method to compute time in milliseconds in matlab tic toc compute time in milliseconds but for windows the first method call time(NULL) measure time in seconds only and clock() measure CPU time not the actual time – user3617694 May 12 '14 at 17:53
  • Take a look at this link: http://web.archive.org/web/20121018065403/http://msdn.microsoft.com/en-us/magazine/cc163996.aspx – rayryeng May 12 '14 at 17:58