testcode.c 691 B

1234567891011121314151617181920
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/time.h>
  4. int main() {
  5. char cmd[100];
  6. fgets(cmd, 100, stdin); // get command
  7. struct timeval start, end;
  8. long long total_time = 0, cnt = 100; // run 100 times
  9. for (int i = 1; i <= cnt; i++) {
  10. gettimeofday(&start, NULL);
  11. system(cmd); // run command
  12. gettimeofday(&end, NULL);
  13. total_time += (end.tv_sec - start.tv_sec) * 1000000 + (end.tv_usec - start.tv_usec); // calculate total time
  14. printf("%d-th time is %f s\n", i, (end.tv_sec - start.tv_sec) + (end.tv_usec - start.tv_usec) / 1000000.);
  15. }
  16. printf("average time is %f s\n", total_time / 1000000. / cnt);
  17. return 0;
  18. }