utils.c 808 B

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