timing.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /// @file
  2. /// @ingroup common_utils
  3. /*************************************************************************
  4. * Copyright (c) 2011 AT&T Intellectual Property
  5. * All rights reserved. This program and the accompanying materials
  6. * are made available under the terms of the Eclipse Public License v1.0
  7. * which accompanies this distribution, and is available at
  8. * https://www.eclipse.org/legal/epl-v10.html
  9. *
  10. * Contributors: Details at https://graphviz.org
  11. *************************************************************************/
  12. #ifndef _WIN32
  13. #include <sys/types.h>
  14. #include <sys/times.h>
  15. #include <sys/param.h>
  16. #ifndef HZ
  17. #define HZ 60
  18. #endif
  19. typedef struct tms mytime_t;
  20. #define GET_TIME(S) times(&(S))
  21. #define DIFF_IN_SECS(S,T) ((S.tms_utime + S.tms_stime - T.tms_utime - T.tms_stime)/(double)HZ)
  22. #else
  23. #include <time.h>
  24. typedef clock_t mytime_t;
  25. #define GET_TIME(S) S = clock()
  26. #define DIFF_IN_SECS(S,T) ((S - T) / (double)CLOCKS_PER_SEC)
  27. #endif
  28. #include <common/types.h>
  29. #include <common/utils.h>
  30. static mytime_t T;
  31. void start_timer(void)
  32. {
  33. GET_TIME(T);
  34. }
  35. double elapsed_sec(void)
  36. {
  37. mytime_t S;
  38. double rv;
  39. GET_TIME(S);
  40. rv = DIFF_IN_SECS(S, T);
  41. return rv;
  42. }