Ask Your Question

Why does it takes so long to capture the frames and store them in memory?

asked 2016-07-07 13:57:04 -0500

alfageme gravatar image

updated 2016-07-09 05:10:05 -0500

pklab gravatar image

So, I'm writting a program that loads a video in memory to determine if it's unstabilized. I realized the program was taking way too long to execute, so I put a timer on my code:

const clock_t begin = std::clock();

int frame_count = 0;
bool should_stop = false;

std::vector <cv::Mat> frames;

    cv::Mat frame;
    cap >> frame;


    if (frame.empty())
        should_stop = true;

std::cout << "Time: " << float( std::clock () - begin ) / CLOCKS_PER_SEC << std::endl;

But this prints times around ~0.70s as opposed as what I observe and measure with the time command:

$ time ./program ~/Desktop/video.mp4
Time: 0.700728s
./program ~/Desktop/video.mp4  0,56s user 0,19s system 2% cpu 36,409 total

Any ideas on why this is happening?

edit retag flag offensive close merge delete


If you combine user and system time, then you got the exact same value. BTW depending on the amount of frames, 0.7 seconds is quite fast I think ...

StevenPuttemans gravatar imageStevenPuttemans ( 2016-07-08 04:28:03 -0500 )edit

The thing is that the total time the program takes to run and return to the shell is 36,409s, leading me to think, the in-code timer measure is not right. And we're talking about ~200 frames, I agree 0.7s will be fast enough, but instead it runs in about 40s!

alfageme gravatar imagealfageme ( 2016-07-08 13:32:07 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2016-07-09 05:07:10 -0500

pklab gravatar image

updated 2016-07-11 14:09:08 -0500

Starting with your clocks should be 1st and last instruction, we all should remember that std::clock() returns the approximate __processor time used by the process__ check the doc. If the process doesn't use the processor than std::clock() doesn't advance, this is the case when you have multi threading or a lot of I/O or sleep. In addiction std::clock() shouldn't depends so much from overall system load.

Test it with a simple std::cout << "Press a Key"; std::cin.get();

The processor time returned by std::clock() is always the same despite of needed time to press the key !!!

NOTE: THIS IS NOT TRUE ON VS 2013. see this. (Really I've same behaviour also with Win764+gcc version 5.1.0 tdm64-1)

Because it looks you are using *nix OS, in your example:

  • 0.7s is the processor time consumed by your process.
  • 40s is the real time elapsed between process start and stop

If you want to measure real time elapsed between 2 events please refer to so called wall clock.

Please note that you can measure time elapsed with OpenCV using cv::getTickCount() and cv::getTickFrequency()

The code below compares processor time, wall time and elapsed time measured with OpenCV

double procTime,wallTime,cvTime;

clock_t clk0,clk1;
std::chrono::high_resolution_clock::time_point chrono0,chrono1;
double cv0,cv1;

clk0 = std::clock();
chrono0 = std::chrono::high_resolution_clock::now();
cv0 = (double)cv::getTickCount();

// do something ...

clk1 = std::clock();
chrono1 = std::chrono::high_resolution_clock::now();
cv1 = (double)cv::getTickCount();

cout << endl << "Processor time (ms) "  
     << 1000.0*float( clk1 - clk0) / CLOCKS_PER_SEC;

     << endl << "Wall time (ms): "
     << std::chrono::duration<double, std::milli>(chron1 - chron0).count();

     << endl << "Wall time using OpenCV (ms):"
     << cvTime = 1000.0*(t1 - t0) / cv::getTickFrequency();
     << endl;
edit flag offensive delete link more


I tried it using the std::clock() functions again and taking into account the declaration of the VideoCapture object and the releases/memory frees, but It keeps giving me the same number D: But the thing is, if I switch to openCV time measure functions, it gives a much more realistic approximation to the run time. Any clue on why the standard library fails at this point?

alfageme gravatar imagealfageme ( 2016-07-11 10:37:53 -0500 )edit

@alfageme please check my new answer

pklab gravatar imagepklab ( 2016-07-11 14:10:58 -0500 )edit

Question Tools

1 follower


Asked: 2016-07-07 13:57:04 -0500

Seen: 282 times

Last updated: Jul 11 '16