2 ***********************************************************************
3 * Copyright (C) 2016 and later: Unicode, Inc. and others.
4 * License & terms of use: http://www.unicode.org/copyright.html#License
5 ***********************************************************************
6 ***********************************************************************
7 * Copyright (c) 2011-2012,International Business Machines
8 * Corporation and others. All Rights Reserved.
9 ***********************************************************************
12 #include "unicode/utimer.h"
19 /* prime number sieve */
21 U_CAPI double uprv_calcSieveTime() {
23 #define SIEVE_SIZE U_LOTS_OF_TIMES /* standardized size */
25 #define SIEVE_SIZE <something_smaller>
30 char sieve[SIEVE_SIZE];
35 for(int j=0;j<SIEVE_SIZE;j++) {
43 printf("init %d: %.9f\n", SIEVE_SIZE,utimer_getDeltaSeconds(&a,&b));
47 for(i=2;i<SIEVE_SIZE/2;i++) {
48 for(k=i*2;k<SIEVE_SIZE;k+=i) {
54 printf("sieve %d: %.9f\n", SIEVE_SIZE,utimer_getDeltaSeconds(&a,&b));
58 for(i=2;i<SIEVE_SIZE && k<SIEVE_PRINT;i++) {
68 for(i=0;i<SIEVE_SIZE;i++) {
71 printf("Primes: %d\n", k);
75 return utimer_getDeltaSeconds(&a,&b);
77 static int comdoub(const void *aa, const void *bb)
79 const double *a = (const double*)aa;
80 const double *b = (const double*)bb;
82 return (*a==*b)?0:((*a<*b)?-1:1);
85 double midpoint(double *times, double i, int n) {
90 return times[(int)fl];
92 return (times[(int)fl]+times[(int)ce])/2;
96 double medianof(double *times, int n, int type) {
99 return midpoint(times,n/4,n);
101 return midpoint(times,n/2,n);
103 return midpoint(times,(n/2)+(n/4),n);
108 double qs(double *times, int n, double *q1, double *q2, double *q3) {
109 *q1 = medianof(times,n,1);
110 *q2 = medianof(times,n,2);
111 *q3 = medianof(times,n,3);
115 U_CAPI double uprv_getMeanTime(double *times, uint32_t *timeCount, double *marginOfError) {
119 /* calculate medians */
120 qsort(times,n,sizeof(times[0]),comdoub);
121 double iqr = qs(times,n,&q1,&q2,&q3);
122 double rangeMin= (q1-(1.5*iqr));
123 double rangeMax = (q3+(1.5*iqr));
125 /* Throw out outliers */
128 printf("iqr: %.9f, q1=%.9f, q2=%.9f, q3=%.9f, max=%.9f, n=%d\n", iqr,q1,q2,q3,(double)-1, n);
130 for(int i=0;i<newN;i++) {
131 if(times[i]<rangeMin || times[i]>rangeMax) {
133 printf("Removing outlier: %.9f outside [%.9f:%.9f]\n", times[i], rangeMin, rangeMax);
135 times[i--] = times[--newN]; // bring down a new value
140 UBool didRemove = false;
142 /* if we removed any outliers, recalculate iqr */
146 printf("removed %d outlier(s), recalculating IQR..\n", n-newN);
151 qsort(times,n,sizeof(times[0]),comdoub);
152 double iqr = qs(times,n,&q1,&q2,&q3);
153 rangeMin= (q1-(1.5*iqr));
154 rangeMax = (q3+(1.5*iqr));
157 /* calculate min/max and mean */
158 double minTime = times[0];
159 double maxTime = times[0];
160 double meanTime = times[0];
161 for(int i=1;i<n;i++) {
162 if(minTime>times[i]) minTime=times[i];
163 if(maxTime<times[i]) maxTime=times[i];
168 /* caculate standard deviation */
170 for(int i=0;i<n;i++) {
173 printf("recalc %d/%d: %.9f\n", i, n, times[i]);
176 sd += (times[i]-meanTime)*(times[i]-meanTime);
178 sd = sqrt(sd/((double)n-1.0));
181 printf("sd: %.9f, mean: %.9f\n", sd, meanTime);
182 printf("min: %.9f, q1=%.9f, q2=%.9f, q3=%.9f, max=%.9f, n=%d\n", minTime,q1,q2,q3,maxTime, n);
183 printf("iqr/sd = %.9f\n", iqr/sd);
186 /* 1.960 = z sub 0.025 */
187 *marginOfError = 1.960 * (sd/sqrt((double)n));
188 /*printf("Margin of Error = %.4f (95%% confidence)\n", me);*/
193 UBool calcSieveTime = FALSE;
194 double meanSieveTime = 0.0;
195 double meanSieveME = 0.0;
197 U_CAPI double uprv_getSieveTime(double *marginOfError) {
198 if(calcSieveTime==FALSE) {
200 uint32_t samples = SAMPLES;
201 double times[SAMPLES];
203 for(int i=0;i<SAMPLES;i++) {
204 times[i] = uprv_calcSieveTime();
206 printf("sieve: %d/%d: %.9f\n", i,SAMPLES, times[i]);
210 meanSieveTime = uprv_getMeanTime(times, &samples,&meanSieveME);
213 if(marginOfError!=NULL) {
214 *marginOfError = meanSieveME;
216 return meanSieveTime;