Calculate the time of particular block of the code. This time used to validate the execution time of the program of particular block of code.
void elapsed_time_display(struct timeval *start, struct timeval *end, char *time_for)
{
double elapsed = 0;
int main()
{
struct timeval time_start, time_end;
int i = 0;
return 0;
}
Vel Elapsed printf time in second = 0.000014 second= 0 Microsecond = 14
Vel Elapsed while time in second = 0.023843 second= 0 Microsecond = 23843
Example:
#include <stdio.h>
#include <sys/time.h>
#include <sys/time.h>
void elapsed_time_display(struct timeval *start, struct timeval *end, char *time_for)
{
double elapsed = 0;
elapsed = (end->tv_sec - start->tv_sec) +
((end->tv_usec - start->tv_usec)/1000000.0);
((end->tv_usec - start->tv_usec)/1000000.0);
printf("\n\n\t\tVel Elapsed %s time in second = %f second= %d Microsecond = %d \n",time_for, elapsed, (int)(end->tv_sec - start->tv_sec), (int)(end->tv_usec - start->tv_usec));
}
int main()
{
struct timeval time_start, time_end;
int i = 0;
gettimeofday(&time_start,NULL);
printf("Calculate printf time\n");
gettimeofday(&time_end,NULL);
printf("Calculate printf time\n");
gettimeofday(&time_end,NULL);
elapsed_time_display(&time_start, &time_end, "printf");
gettimeofday(&time_start,NULL);
while(i < 10000000) {
i++;
}
gettimeofday(&time_end,NULL);
while(i < 10000000) {
i++;
}
gettimeofday(&time_end,NULL);
elapsed_time_display(&time_start, &time_end, "while");
return 0;
}
Output:
Calculate printf time
Vel Elapsed printf time in second = 0.000014 second= 0 Microsecond = 14
Vel Elapsed while time in second = 0.023843 second= 0 Microsecond = 23843
No comments:
Post a Comment