Wondering Get Cpu Time Start Function Cpu Time End Function Take Times Figure Total Time N Q37262877

iwas wondering how i can get the CPU time from the start of afunction and the CPU time of the end of the function and then takeboth times to figure out the total time. All this needs to be inSeconds and Milliseconds.


Answer


If you are using C or C++, you will have to include time.hheader file.

#include <time.h>

void my_function() {

// Some code logic of the function here

}

int main() {

// assuming you want to calculate the execution time formy_function

clock_t startTime, endTime;

double totalDuration;

startTime = clock();

my_function();

endTime = clock();

totalDuration = (double(endTime – startTime))/ CLOCKS_PER_SEC //this will give you totalDuration in secs

}

totalDuration is all you

OR
OR

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.