You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
2.7 KiB

  1. /*
  2. * ftimer.c - Estimate the time (in seconds) used by a function f
  3. *
  4. * Copyright (c) 2002, R. Bryant and D. O'Hallaron, All rights reserved.
  5. * May not be used, modified, or copied without permission.
  6. *
  7. * Function timers that estimate the running time (in seconds) of a function f.
  8. * ftimer_itimer: version that uses the interval timer
  9. * ftimer_gettod: version that uses gettimeofday
  10. */
  11. #include <stdio.h>
  12. #include <sys/time.h>
  13. #include "ftimer.h"
  14. /* function prototypes */
  15. static void init_etime(void);
  16. static double get_etime(void);
  17. /*
  18. * ftimer_itimer - Use the interval timer to estimate the running time
  19. * of f(argp). Return the average of n runs.
  20. */
  21. double ftimer_itimer(ftimer_test_funct f, void *argp, int n)
  22. {
  23. double start, tmeas;
  24. int i;
  25. init_etime();
  26. start = get_etime();
  27. for (i = 0; i < n; i++)
  28. f(argp);
  29. tmeas = get_etime() - start;
  30. return tmeas / n;
  31. }
  32. /*
  33. * ftimer_gettod - Use gettimeofday to estimate the running time of
  34. * f(argp). Return the average of n runs.
  35. */
  36. double ftimer_gettod(ftimer_test_funct f, void *argp, int n)
  37. {
  38. int i;
  39. struct timeval stv, etv;
  40. double diff;
  41. gettimeofday(&stv, NULL);
  42. for (i = 0; i < n; i++)
  43. f(argp);
  44. gettimeofday(&etv,NULL);
  45. diff = 1E3*(etv.tv_sec - stv.tv_sec) + 1E-3*(etv.tv_usec-stv.tv_usec);
  46. diff /= n;
  47. return (1E-3*diff);
  48. }
  49. /*
  50. * Routines for manipulating the Unix interval timer
  51. */
  52. /* The initial value of the interval timer */
  53. #define MAX_ETIME 86400
  54. /* static variables that hold the initial value of the interval timer */
  55. static struct itimerval first_u; /* user time */
  56. static struct itimerval first_r; /* real time */
  57. static struct itimerval first_p; /* prof time*/
  58. /* init the timer */
  59. static void init_etime(void)
  60. {
  61. first_u.it_interval.tv_sec = 0;
  62. first_u.it_interval.tv_usec = 0;
  63. first_u.it_value.tv_sec = MAX_ETIME;
  64. first_u.it_value.tv_usec = 0;
  65. setitimer(ITIMER_VIRTUAL, &first_u, NULL);
  66. first_r.it_interval.tv_sec = 0;
  67. first_r.it_interval.tv_usec = 0;
  68. first_r.it_value.tv_sec = MAX_ETIME;
  69. first_r.it_value.tv_usec = 0;
  70. setitimer(ITIMER_REAL, &first_r, NULL);
  71. first_p.it_interval.tv_sec = 0;
  72. first_p.it_interval.tv_usec = 0;
  73. first_p.it_value.tv_sec = MAX_ETIME;
  74. first_p.it_value.tv_usec = 0;
  75. setitimer(ITIMER_PROF, &first_p, NULL);
  76. }
  77. /* return elapsed real seconds since call to init_etime */
  78. static double get_etime(void) {
  79. struct itimerval v_curr;
  80. struct itimerval r_curr;
  81. struct itimerval p_curr;
  82. getitimer(ITIMER_VIRTUAL, &v_curr);
  83. getitimer(ITIMER_REAL,&r_curr);
  84. getitimer(ITIMER_PROF,&p_curr);
  85. return (double) ((first_p.it_value.tv_sec - r_curr.it_value.tv_sec) +
  86. (first_p.it_value.tv_usec - r_curr.it_value.tv_usec)*1e-6);
  87. }