/* Example of using an inteval timer on most Unix machines. */ #include <stdio.h> #include <math.h> #include <unistd.h> #include <stdlib.h> #include <time.h> #include <sys/time.h> /* declare some stuff for the timer. */ struct timeval start_time, end_time; main(int argc, char *argv[]) { /* Declare some values to be used with the timer. */ double start_count, end_count; double elapsed_time; /* measured time in microseconds */ /* Declare your other variables used in the program. */ int i,n; float a; if (argc > 1) sscanf(argv[1],"%d",&n); else n = 100000; a = 101.1; /* Start the timer by reading its current value. */ gettimeofday(&start_time,NULL); /* Stuff to be measured */ for (i=0;i< n;i++){ a = i * a / 10; } /* Measure the elapsed time by subtracting the start value from the current value. */ gettimeofday(&end_time,NULL); start_count = (double) start_time.tv_sec + 1.e-6 * (double) start_time.tv_usec; end_count = (double) end_time.tv_sec + 1.e-6 * (double) end_time.tv_usec; elapsed_time = (end_count - start_count); printf("The total elapsed time is: %f seconds\n",elapsed_time); }