C programming question below:
My program is suppose to populate an array with 100,000 numbers 1 - 100,000 in descending order, then sort the array in ascending order using bubble sort and to calculate the time it took to do so. I've got everything working except the time portion. Here's what i have: ``` int bubTime(int *A) { int total_time; struct timeval start_clock,stop_clock; populateArr(A); gettimeofday(&start_clock,NULL); bubSort(A); gettimeofday(&stop_clock,NULL); total_time = stop_clock.tv_sec - start_clock.tv_sec; printf("Total Time: %d\n",total_time); return total_time; } ``` i get the total time of 0 seconds -.- am i missing something?? i have the includes ``` #include <stdlib.h> #include <stdio.h> #include <sys/time.h> #include <time.h> ``` And heres my output if that helps any
double difftime(time_t time1, time_t time2) Returns the difference of seconds between time1 and time2 (time1-time2).
But I think you might want to work with: clock_t clock(void) Returns the processor clock time used since the beginning of an implementation-defined era (normally the beginning of the program).
I wonder if i need to declare total_time as of type timeval and then cast it into a int like they did in that example using clock_t
If not then the difference one should work
The problem with the standard time objects is they don't deal with the accuracy level of the cpu speed. Note it is accurate to 1 second.
Join our real-time social learning platform and learn together with your friends!