Advertisement
Guest User

Main Example

a guest
Jun 22nd, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <sys/time.h>
  5. #ifdef __MACH__
  6. #include <mach/clock.h>
  7. #include <mach/mach.h>
  8. #endif
  9.  
  10. #define MAXBUF 10
  11.  
  12. static void gettime (struct timespec *t) {
  13. #ifdef __MACH__
  14.     clock_serv_t cclock;
  15.     mach_timespec_t mts;
  16.     host_get_clock_service(mach_host_self(), REALTIME_CLOCK, &cclock);
  17.     clock_get_time(cclock, &mts);
  18.     mach_port_deallocate(mach_task_self(), cclock);
  19.     t->tv_sec = mts.tv_sec;
  20.     t->tv_nsec = mts.tv_nsec;
  21. #else
  22.     clock_gettime(CLOCK_REALTIME, t);
  23. #endif
  24. }
  25.  
  26. int main (void) {
  27.     FILE *fp;
  28.     struct timespec tic, toc;
  29.     char *executableName = "./a.out";
  30.     char answer[MAXBUF];
  31.    
  32.     gettime(&tic);
  33.     if ((fp = popen(executableName, "r")) == NULL) {
  34.         fprintf(stderr, "The file couldn't be opened.\n");
  35.         return 1;
  36.     }
  37.     gettime(&toc);
  38.    
  39.     fgets(answer, MAXBUF, fp);
  40.     double elapsed = (double)(toc.tv_nsec - tic.tv_nsec) / 1000000000;
  41.     fprintf(stdout, "The program says %s, and took %fs to run!\n", answer, elapsed);
  42.    
  43.     pclose(fp);
  44.     return 0;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement