]>
git.saurik.com Git - apple/icu.git/blob - icuSources/test/perf/howExpensiveIs/sieve.cpp
2 **********************************************************************
3 * Copyright (c) 2011-2012,International Business Machines
4 * Corporation and others. All Rights Reserved.
5 **********************************************************************
8 #include "unicode/utimer.h"
15 /* prime number sieve */
17 U_CAPI
double uprv_calcSieveTime() {
19 #define SIEVE_SIZE U_LOTS_OF_TIMES /* standardized size */
21 #define SIEVE_SIZE <something_smaller>
26 char sieve
[SIEVE_SIZE
];
31 for(int j
=0;j
<SIEVE_SIZE
;j
++) {
39 printf("init %d: %.9f\n", SIEVE_SIZE
,utimer_getDeltaSeconds(&a
,&b
));
43 for(i
=2;i
<SIEVE_SIZE
/2;i
++) {
44 for(k
=i
*2;k
<SIEVE_SIZE
;k
+=i
) {
50 printf("sieve %d: %.9f\n", SIEVE_SIZE
,utimer_getDeltaSeconds(&a
,&b
));
54 for(i
=2;i
<SIEVE_SIZE
&& k
<SIEVE_PRINT
;i
++) {
64 for(i
=0;i
<SIEVE_SIZE
;i
++) {
67 printf("Primes: %d\n", k
);
71 return utimer_getDeltaSeconds(&a
,&b
);
73 static int comdoub(const void *aa
, const void *bb
)
75 const double *a
= (const double*)aa
;
76 const double *b
= (const double*)bb
;
78 return (*a
==*b
)?0:((*a
<*b
)?-1:1);
81 double midpoint(double *times
, double i
, int n
) {
86 return times
[(int)fl
];
88 return (times
[(int)fl
]+times
[(int)ce
])/2;
92 double medianof(double *times
, int n
, int type
) {
95 return midpoint(times
,n
/4,n
);
97 return midpoint(times
,n
/2,n
);
99 return midpoint(times
,(n
/2)+(n
/4),n
);
104 double qs(double *times
, int n
, double *q1
, double *q2
, double *q3
) {
105 *q1
= medianof(times
,n
,1);
106 *q2
= medianof(times
,n
,2);
107 *q3
= medianof(times
,n
,3);
111 U_CAPI
double uprv_getMeanTime(double *times
, uint32_t *timeCount
, double *marginOfError
) {
115 /* calculate medians */
116 qsort(times
,n
,sizeof(times
[0]),comdoub
);
117 double iqr
= qs(times
,n
,&q1
,&q2
,&q3
);
118 double rangeMin
= (q1
-(1.5*iqr
));
119 double rangeMax
= (q3
+(1.5*iqr
));
121 /* Throw out outliers */
124 printf("iqr: %.9f, q1=%.9f, q2=%.9f, q3=%.9f, max=%.9f, n=%d\n", iqr
,q1
,q2
,q3
,(double)-1, n
);
126 for(int i
=0;i
<newN
;i
++) {
127 if(times
[i
]<rangeMin
|| times
[i
]>rangeMax
) {
129 printf("Removing outlier: %.9f outside [%.9f:%.9f]\n", times
[i
], rangeMin
, rangeMax
);
131 times
[i
--] = times
[--newN
]; // bring down a new value
136 UBool didRemove
= false;
138 /* if we removed any outliers, recalculate iqr */
142 printf("removed %d outlier(s), recalculating IQR..\n", n
-newN
);
147 qsort(times
,n
,sizeof(times
[0]),comdoub
);
148 double iqr
= qs(times
,n
,&q1
,&q2
,&q3
);
149 rangeMin
= (q1
-(1.5*iqr
));
150 rangeMax
= (q3
+(1.5*iqr
));
153 /* calculate min/max and mean */
154 double minTime
= times
[0];
155 double maxTime
= times
[0];
156 double meanTime
= times
[0];
157 for(int i
=1;i
<n
;i
++) {
158 if(minTime
>times
[i
]) minTime
=times
[i
];
159 if(maxTime
<times
[i
]) maxTime
=times
[i
];
164 /* caculate standard deviation */
166 for(int i
=0;i
<n
;i
++) {
169 printf("recalc %d/%d: %.9f\n", i
, n
, times
[i
]);
172 sd
+= (times
[i
]-meanTime
)*(times
[i
]-meanTime
);
174 sd
= sqrt(sd
/((double)n
-1.0));
177 printf("sd: %.9f, mean: %.9f\n", sd
, meanTime
);
178 printf("min: %.9f, q1=%.9f, q2=%.9f, q3=%.9f, max=%.9f, n=%d\n", minTime
,q1
,q2
,q3
,maxTime
, n
);
179 printf("iqr/sd = %.9f\n", iqr
/sd
);
182 /* 1.960 = z sub 0.025 */
183 *marginOfError
= 1.960 * (sd
/sqrt((double)n
));
184 /*printf("Margin of Error = %.4f (95%% confidence)\n", me);*/
189 UBool calcSieveTime
= FALSE
;
190 double meanSieveTime
= 0.0;
191 double meanSieveME
= 0.0;
193 U_CAPI
double uprv_getSieveTime(double *marginOfError
) {
194 if(calcSieveTime
==FALSE
) {
196 uint32_t samples
= SAMPLES
;
197 double times
[SAMPLES
];
199 for(int i
=0;i
<SAMPLES
;i
++) {
200 times
[i
] = uprv_calcSieveTime();
202 printf("sieve: %d/%d: %.9f\n", i
,SAMPLES
, times
[i
]);
206 meanSieveTime
= uprv_getMeanTime(times
, &samples
,&meanSieveME
);
209 if(marginOfError
!=NULL
) {
210 *marginOfError
= meanSieveME
;
212 return meanSieveTime
;