#include<time.h>To actually obtain the execution time, you must measure the the time when the algorithm starts and the time when the algorithm stops. The difference between these values will provide the execution time of the algorithm.
Execution time formula dt - the execution time of the algorithm tf - the time value measured when the algorithm stops ts - the time value measured when the algorithm starts |
The times tf and ts will be measured using the clock_t clock() function (who will return the number of ticks since the program was started). Since the difference will be in ticks, you must divide the result with the constant CLOCKS_PER_SEC in order to get the result in seconds.
#include<stdio.h> #include<time.h> int main(void) { clock_t startTime, endTime; /*Measures ts*/ startTime = clock(); /*Insert your algorithm here * * * * */ /*Measures tf*/ endTime = clock(); /*Calculates and prints the time in ticks*/ printf("Time in ticks: %ld",(endTime-startTime)); /*Calculates and prints the time in seconds*/ printf(" %f",(double)(endTime-startTime)/CLOCKS_PER_SEC); return 0; }
No comments:
Post a Comment
Got a question regarding something in the article? Leave me a comment and I will get back at you as soon as I can!