util.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /* FLAC - Free Lossless Audio Codec
  2. * Copyright (C) 2015-2016 Xiph.Org Foundation
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * - Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. *
  11. * - Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * - Neither the name of the Xiph.org Foundation nor the names of its
  16. * contributors may be used to endorse or promote products derived from
  17. * this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
  23. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  24. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  25. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  26. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  27. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  28. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #include <config.h>
  32. #include <stdlib.h>
  33. #include "util.h"
  34. #if defined _WIN32
  35. #include <windows.h>
  36. static double
  37. counter_diff (const LARGE_INTEGER * start, const LARGE_INTEGER * end)
  38. {
  39. LARGE_INTEGER diff, freq;
  40. QueryPerformanceFrequency(&freq);
  41. diff.QuadPart = end->QuadPart - start->QuadPart;
  42. return (double)diff.QuadPart/(double)freq.QuadPart;
  43. }
  44. double
  45. benchmark_function (void (*testfunc) (void), unsigned count)
  46. {
  47. LARGE_INTEGER start, end;
  48. unsigned k;
  49. QueryPerformanceCounter (&start) ;
  50. for (k = 0 ; k < count ; k++)
  51. testfunc();
  52. QueryPerformanceCounter (&end) ;
  53. return counter_diff (&start, &end) / count ;
  54. } /* benchmark_function */
  55. #elif defined FLAC__SYS_DARWIN
  56. #include <mach/mach_time.h>
  57. static double
  58. counter_diff (const uint64_t * start, const uint64_t * end)
  59. {
  60. mach_timebase_info_data_t t_info;
  61. mach_timebase_info(&t_info);
  62. uint64_t duration = *end - *start;
  63. return duration * ((double)t_info.numer/(double)t_info.denom);
  64. }
  65. double
  66. benchmark_function (void (*testfunc) (void), unsigned count)
  67. {
  68. uint64_t start, end;
  69. unsigned k;
  70. start = mach_absolute_time();
  71. for (k = 0 ; k < count ; k++)
  72. testfunc();
  73. end = mach_absolute_time();
  74. return counter_diff (&start, &end) / count ;
  75. } /* benchmark_function */
  76. #elif defined HAVE_CLOCK_GETTIME
  77. #include <time.h>
  78. #include <sys/time.h>
  79. static double
  80. timespec_diff (const struct timespec * start, const struct timespec * end)
  81. { struct timespec diff;
  82. if (end->tv_nsec - start->tv_nsec < 0)
  83. { diff.tv_sec = end->tv_sec - start->tv_sec - 1 ;
  84. diff.tv_nsec = 1000000000 + end->tv_nsec - start->tv_nsec ;
  85. }
  86. else
  87. { diff.tv_sec = end->tv_sec - start->tv_sec ;
  88. diff.tv_nsec = end->tv_nsec-start->tv_nsec ;
  89. } ;
  90. return diff.tv_sec + 1e-9 * diff.tv_nsec ;
  91. }
  92. double
  93. benchmark_function (void (*testfunc) (void), unsigned count)
  94. { struct timespec start, end;
  95. unsigned k ;
  96. clock_gettime (CLOCK_PROCESS_CPUTIME_ID, &start) ;
  97. for (k = 0 ; k < count ; k++)
  98. testfunc () ;
  99. clock_gettime (CLOCK_PROCESS_CPUTIME_ID, &end) ;
  100. return timespec_diff (&start, &end) / count ;
  101. } /* benchmark_function */
  102. #else
  103. #include <time.h>
  104. #include <sys/time.h>
  105. static double
  106. timeval_diff (const struct timeval * start, const struct timeval * end)
  107. { struct timeval diff;
  108. if (end->tv_usec - start->tv_usec < 0)
  109. { diff.tv_sec = end->tv_sec - start->tv_sec - 1 ;
  110. diff.tv_usec = 1000000 + end->tv_usec - start->tv_usec ;
  111. }
  112. else
  113. { diff.tv_sec = end->tv_sec - start->tv_sec ;
  114. diff.tv_usec = end->tv_usec-start->tv_usec ;
  115. } ;
  116. return diff.tv_sec + 1e-6 * diff.tv_usec ;
  117. }
  118. double
  119. benchmark_function (void (*testfunc) (void), unsigned count)
  120. { struct timeval start, end;
  121. unsigned k ;
  122. gettimeofday(&start, NULL);
  123. for (k = 0 ; k < count ; k++)
  124. testfunc () ;
  125. gettimeofday(&end, NULL);
  126. return timeval_diff (&start, &end) / count ;
  127. } /* benchmark_function */
  128. #endif
  129. static int
  130. double_cmp (const void * a, const void * b)
  131. { const double * pa = (double *) a ;
  132. const double * pb = (double *) b ;
  133. return pa [0] < pb [0] ;
  134. } /* double_cmp */
  135. void
  136. benchmark_stats (bench_stats * stats)
  137. { double sum, times [stats->run_count] ;
  138. unsigned k ;
  139. for (k = 0 ; k < stats->run_count ; k++)
  140. times [k] = benchmark_function (stats->testfunc, stats->loop_count) ;
  141. qsort (times, stats->run_count, sizeof (times [0]), double_cmp) ;
  142. sum = 0.0 ;
  143. stats->min_time = stats->max_time = times [0] ;
  144. for (k = 0 ; k < stats->run_count ; k++)
  145. { stats->min_time = stats->min_time < times [k] ? stats->min_time : times [k] ;
  146. stats->max_time = stats->max_time > times [k] ? stats->max_time : times [k] ;
  147. sum += times [k] ;
  148. }
  149. stats->mean_time = sum / stats->run_count ;
  150. if (stats->run_count & 1)
  151. stats->median_time = times [(stats->run_count + 1) / 2] ;
  152. else
  153. stats->median_time = 0.5 * (times [stats->run_count / 2] + times [(stats->run_count / 2) + 1]) ;
  154. return ;
  155. } /* benchmark_stats */