Hello , I can't figure how to properly measure time execution when I have 2 loops and I want to measure the time for the innermost loop:

for (int i = 1; i <= N; i++) {

.....

clock_t begin = clock();

for (int j =1; j<= M; j++){  //want to measure time for this loop for each "i"

...
}
clock_t end = clock();
}

Like this it only measures correct time for the first loop.

Because your end variable is outside of the inner loop.

Move it inside and do your calculations.

I know that for i = 1 time is around 0.3sec.
So, I am expecting this time for every loop.Instead , I am taking:

0.3
5.85
7
5.2
...

If you are using a multi-process operating system such as MS-Windows or *nix there are lots of things outside the control of your program that could cause such behavior, such as some other process could consume more CPU time, or the os might be flushing data to the file system.

If you want the time of the run of the inner loop for every iteration of the out loop you should be able to do this

//...
std::vector<std::pair<clock_t, clock_t>> loopTimes
for (int i = 0; i < N; i++)
{
    cloct_t begin = clock();
    for (int j = 0; j < X; j++)
    {
        // do stuff
    }
    clock_t end = clock();
    loopTimes.push_back(std::make_pair(begin, end));
}

// get times
for (size_t i = 0; i < loopTimes.size(); i++)
{
    std::cout << "Sample " << i << ": " << (float)(loopTimes[i].first - loopTimes[i].second) / CLOCKS_PER_SEC << " Seconds\n"
}

Ok, thanks ! I can't test it now , until next week ,I will
let you know.

The actual code is meaningful because it depends from too many things.I can show only something like :

    for (int i = 1; i <= NbS; i++) {

            //call a function
            myfunction(.....);

            //timiming for computing the X vector
            clock_t begin = clock();
            clock_t end;

            for (int k = 0 ; k < NbC; k++) {

                X[k] = 0;
                for (int l = 0; l < NbE ; l++) {

                        size_t c = 0;
                        float mysum=0;
                        while ( .... ) {

                            mysum += ....
                            c++;
                    } //end of  while loop



                X[ k ] += mysum * B[ l ];

                }//end of l loop

            } 
            end = clock();
            cout << "\nUsed time : " <<endl;



        }

Edited 2 Years Ago by glao

How are you outputting the time used? Line 31 just has cout << "\nUsed time : " <<endl;. Are you dealing with threads at all? Do youknow if the execution times should be consistent for each iteration of the outer loop?

If you want to measure the time of an iteration in the inner loop, then calculate and output it in the inner.

I said that in the first reply and it still stands.

How are you outputting the time used?

Sorry , copy paste..

cout << "\nUsed time : " << double(get_time_(end,begin)) <<endl;

double get_time(clock_t clock1,clock_t clock2)
{
    double diff_ticks = clock1-clock2;
    double diff_s= ( (diff_ticks*1000) / CLOCKS_PER_SEC ) *1e-3; 

    return diff_s;

}

Are you dealing with threads at all? Do youknow if the execution times should be consistent for each iteration of the outer loop?

No , I am not using any threads.
The time should be consistent, yes.

If you want to measure the time of an iteration in the inner loop, then calculate and output it in the inner.

I did that as I told you but still the same.

Okay, I'm confused.

You start your timer/clock in the outermost for loop and you end it in the same loop, I'm just not seeing how that can possibly be measuring an iteration in your innermost loop.

This article has been dead for over six months. Start a new discussion instead.