Calculate execution time when sleep() is used

后端 未结 5 1750
难免孤独
难免孤独 2021-01-23 11:30

I am trying to read data from the ADC in the Beagle Bone, running Angstrom Linux distribution. I need to use a delay mechanism like sleep(), to only read samples at

5条回答
  •  被撕碎了的回忆
    2021-01-23 12:02

    The solution to calculate execution time is to get the timestamp at the beginning of your program and at the end. Then make the difference.

    #include 
    #include 
    
    int main() {
    
        time_t begin;
        time(&begin);
    
      // Somethings
    
       time_t end;
       time(&end);
    
      printf("Execution time %f\n", difftime(end, begin));
      return (0);
    }
    

    EDIT:

    #include 
    #include 
    #include 
    
    int main() {
    
      struct timeval  tv;
      gettimeofday(&tv, NULL);
    
    double begin =
      (tv.tv_sec) * 1000 + (tv.tv_usec) / 1000 ;
    
    
     sleep(2);
    
     gettimeofday(&tv, NULL);
    
    double end =
      (tv.tv_sec) * 1000 + (tv.tv_usec) / 1000 ;
    
      printf("Execution time %f\n", end - begin);
      return (0);
    }
    

提交回复
热议问题