Calculate Execution Time in C ?

Status
Not open for further replies.

infantheartlyje

Newbie level 2
Joined
Oct 14, 2011
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,295
How to calculate Execution time in milliseconds?
if i use time() function i can get the result only in seconds .
What about clock function ? How to print the difference using clock_t variable?

ts=clock();
quicksort(x,0,size-1);
printf("Time elapsed: %f\n", ((double)clock() - ts) / CLOCKS_PER_SEC);

its only resulting in Seconds .
 

To convert the Epoch into microseconds you must add both members of the structure, by first converting seconds contained in timeval.tv_sec and adding timeval.tv_usec both contained in the timeval structure.

For wall-clock time:

Code:
#include <sys/time.h>
#include <iostream>

int main(int argc, char **argv) {
    // ProcessTime example
    struct timeval startTime;
    struct timeval endTime;
    // get the current time
    // - NULL because we don't care about time zone
    gettimeofday(&startTime, NULL);
    // algorithm goes here
    what_you_want_to_time();
    // get the end time
    gettimeofday(&endTime, NULL);
    // calculate time in microseconds
    double tS = startTime.tv_sec*1000000 + (startTime.tv_usec);
    double tE = endTime.tv_sec*1000000  + (endTime.tv_usec);
    std::cout << "Total Time Taken: " << tE - tS << std::endl;
    return 0;
}

For process time:

Code:
#include <sys/time.h>
#include <iostream>
int main(int argc, char **argv) {
    // ProcessTime example
    struct timeval startTime;
    struct timeval endTime;
    //structure for rusage
    struct rusage ru;
    // get the current time
    // - RUSAGE_SELF for current process
    // - RUSAGE_CHILDREN for *terminated* subprocesses
    getrusage(RUSAGE_SELF, &ru);
    startTime = ru.ru_utime;
    // algorithm goes here
    what_you_want_to_time();
    // get the end time
    getrusage(RUSAGE_SELF, &ru);
    endTime = ru.ru_utime;
    // calculate time in microseconds
    double tS = startTime.tv_sec*1000000 + (startTime.tv_usec);
    double tE = endTime.tv_sec*1000000  + (endTime.tv_usec);
    std::cout << "Total Time Taken: " << tE - tS << std::endl;
    return 0;
}

Hope the info helps,

BigDog
 


Am using C in GCC compiler . Not C++.
After compiling this program i got these errors.

qstchk1.c: In function ‘main’:
qstchk1.c:10:15: error: storage size of ‘ru’ isn’t known
qstchk1.c:18:11: error: ‘RUSAGE_SELF’ undeclared (first use in this function)
qstchk1.c:18:11: note: each undeclared identifier is reported only once for each function it appears in
 

Am using C in GCC compiler . Not C++.

The only C++ features in the code are the <iostream> routines. Substitute your own "printf()" for the "cout" statements.


Try adding the following header file:

#include <sys/resource.h>

The structure "rusage" is contained within "resource.h" on some systems.


BigDog
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…