| 1234567891011121314151617181920 |
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/time.h>
- int main() {
- char cmd[100];
- fgets(cmd, 100, stdin); // get command
- struct timeval start, end;
- long long total_time = 0, cnt = 100; // run 100 times
- for (int i = 1; i <= cnt; i++) {
- gettimeofday(&start, NULL);
- system(cmd); // run command
- gettimeofday(&end, NULL);
- total_time += (end.tv_sec - start.tv_sec) * 1000000 + (end.tv_usec - start.tv_usec); // calculate total time
- printf("%d-th time is %f s\n", i, (end.tv_sec - start.tv_sec) + (end.tv_usec - start.tv_usec) / 1000000.);
- }
- printf("average time is %f s\n", total_time / 1000000. / cnt);
- return 0;
- }
|