Advertisement
4da

measure function exec time

4da
Jul 7th, 2011
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.74 KB | None | 0 0
  1. #include <sys/time.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <math.h>
  5.  
  6. /* Return 1 if the difference is negative, otherwise 0.  */
  7. int timeval_subtract(struct timeval *result, struct timeval *t2, struct timeval *t1)
  8. {
  9.     long int diff = (t2->tv_usec + 1000000 * t2->tv_sec) - (t1->tv_usec + 1000000 * t1->tv_sec);
  10.     result->tv_sec = diff / 1000000;
  11.     result->tv_usec = diff % 1000000;
  12.  
  13.     return (diff<0);
  14. }
  15.  
  16.  
  17. int main()
  18. {
  19.     struct timeval tvBegin, tvEnd, tvDiff;
  20.  
  21.     gettimeofday(&tvBegin, NULL);
  22.  
  23.     yourFunction();    
  24.  
  25.     gettimeofday(&tvEnd, NULL);
  26.  
  27.  
  28.     // разница
  29.     timeval_subtract(&tvDiff, &tvEnd, &tvBegin);
  30.     printf("%ld.%06ld\n", tvDiff.tv_sec, tvDiff.tv_usec);
  31.  
  32.     return 0;
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement