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