Calculation time elapsed by a particular function in C program

11,012

Solution 1

Method : 1

To calculate total time taken by program You can use linux utility "time".

    Lets your program name is test.cpp.
    $g++ -o test test.cpp
    $time ./test

    Output will be like :
    real    0m11.418s
    user    0m0.004s
    sys 0m0.004s

Method : 2

You can also use linux profiling method "gprof" to find the time by different functions.

First you have to compile the program with "-pg" flag.

    $g++ -pg -o test test.cpp
    $./test
    $gprof test gmon.out

PS : gmon.out is default file created by gprof

Solution 2

Not sure if you tried the following. I know your original post says that you have tried utilizing the CLOCKS_PER_SEC. Using CLOCKS_PER_SEC and doing (stop-start)/CLOCKS_PER_SEC will allow you get seconds. The double will provide more precision.

#include <time.h>

main()
{
clock_t launch = clock();
//do work
clock_t done = clock();
double diff = (done - launch) / CLOCKS_PER_SEC;
}

Solution 3

You can call gettimeofday function in Linux and timeGetTime in Windows. Call these functions before calling your sorting function and after calling your sorting function and take the difference.

Please check the man page for further details. If you are still unable to get some tangible data (as the time taken may be too small due to smaller data sets), better to try to measure the time together for 'n' number of iterations and then deduce the time for a single run or increase the size of the data set to be sorted.

Solution 4

The reason to get Zeroas the result is likely the poor resolution of the time source you're using. These time sources typically increment by some 10 to 20 ms. This is poor but that's the way they work. When your sorting is done in less that this time increment, the result will be zero. You may increase this resultion into the 1 ms regime by increasing the systems interrupt frequency. There is no standard way to accomplish this for windows and Linux. They have their individual way. An even higher resolution can be obtained by a high frequency counter. Windows and Linux do provide access to such counters, but again, the code may look slightly different.

If you deserve one piece of code to run on windows and linux, I'd recommend to perform the time measurement in a loop. Run the code to measure hundreds or even more times in a loop and capture the time outside the loop. Divide the captured time by the numer of loop cycles and have the result.

Of course: This is for evaluation only. You don't want to have that in final code. And: Taking into account that the time resolution is in the 1 to 20 ms you should make a good choice of the total time to go for to get decent resolution of you measurement. (Hint: Adjust the loop count to let it go for at least a second or so.)

Example:

clock_t start, end;

printf("THE LIST BEFORE SORTING IS(UNSORTED LIST):\n");
printlist(arr,n);

start = clock();
for(int i = 0; i < 100; i++){ 
  mergesort(extarr,0,n-1);
}
end   = clock();
double diff = (end - start) / CLOCKS_PER_SEC;

// and so on...

printf("THE LIST AFTER SORTING BY MERGE SORT IS(SORTED LIST):\n");
printlist(extarr,n);
quicksort(arr,0,n-1);
printf("THE LIST AFTER SORTING BY QUICK SORT IS(SORTED LIST):\n");
printlist(arr,n);
Share:
11,012
Mcolorz
Author by

Mcolorz

Updated on June 04, 2022

Comments

  • Mcolorz
    Mcolorz almost 2 years

    I have a code in which i want to calculate the time taken by two sorting algorithms merge sort and quick sort to sort N numbers in microseconds or more precise. The two times thus calculated will then we outputted to the terminal. Code(part of code):

    printf("THE LIST BEFORE SORTING IS(UNSORTED LIST):\n");
    printlist(arr,n);
    mergesort(extarr,0,n-1);
    printf("THE LIST AFTER SORTING BY MERGE SORT IS(SORTED LIST):\n");
    printlist(extarr,n);
    quicksort(arr,0,n-1);
    printf("THE LIST AFTER SORTING BY QUICK SORT IS(SORTED LIST):\n");
    printlist(arr,n);
    

    Help me by providing that how it will be done.I have tried clock_t by taking two variables as start stop and keeping them above and below the function call respectively but this doesnt help at all and always print out the its difference as zero. Please suggest some other methods or function keeping in mind that it has no problem running in any type of OS. Thanks for any help in advance.

  • goji
    goji over 11 years
    Good example for you, including how to calculate the difference of the 2 timeval structs returned by gettimeofday(). mpp.mpg.de/~huber/util/timevaldiff.c
  • Mcolorz
    Mcolorz over 11 years
    @jay I want to make a program that will run in both OS. Not want that if i run this OS than have to do this,if i run that OS than have to do that
  • Sergey L.
    Sergey L. over 11 years
    Unfortunately there is no way around that when using system calls. Windows does not support the full POSIX standard so you will have to resort to different system calls/interfaces.
  • Nandyy
    Nandyy about 9 years
    @Troy For loops running at less than 1000 microseconds, the function timevaldiff will return 0