utils.c 829 B

1234567891011121314151617181920212223242526272829
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <sys/time.h>
  4. #include <malloc.h>
  5. #include "utils.h"
  6. /**
  7. * Calculate a duration
  8. *
  9. * @param start start of the computing or null if
  10. * this is the first time this function is inoked
  11. *
  12. * @return current time
  13. */
  14. struct timeval* showTime(struct timeval* start)
  15. {
  16. struct timeval* stop;
  17. long tv_sec;
  18. long tv_usec;
  19. stop = (struct timeval*)malloc(sizeof(struct timeval));
  20. gettimeofday(stop, 0);
  21. if (!start) return stop;
  22. tv_sec = (start->tv_usec > stop->tv_usec) ? stop->tv_sec - start->tv_sec - 1 : stop->tv_sec - start->tv_sec;
  23. tv_usec = (start->tv_usec > stop->tv_usec) ? 1000000 + stop->tv_usec - start->tv_usec : stop->tv_usec - start->tv_usec;
  24. fprintf(stderr, "%lus %lums %luµs\n", tv_sec, tv_usec/1000, tv_sec % 1000);
  25. return stop;
  26. }