]>
git.saurik.com Git - apple/icu.git/blob - icuSources/test/perf/howExpensiveIs/sieve.cpp
2 **********************************************************************
3 * Copyright (c) 2011,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("Knocking out: %.9f from [%.9f:%.9f]\n", times
[i
], rangeMin
, rangeMax
);
131 times
[i
--] = times
[--newN
]; // bring down a new value
135 /* if we removed any outliers, recalculate iqr */
138 printf("Kicked out %d, retrying..\n", n
-newN
);
142 qsort(times
,n
,sizeof(times
[0]),comdoub
);
143 double iqr
= qs(times
,n
,&q1
,&q2
,&q3
);
144 rangeMin
= (q1
-(1.5*iqr
));
145 rangeMax
= (q3
+(1.5*iqr
));
148 /* calculate min/max and mean */
149 double minTime
= times
[0];
150 double maxTime
= times
[0];
151 double meanTime
= times
[0];
152 for(int i
=1;i
<n
;i
++) {
153 if(minTime
>times
[i
]) minTime
=times
[i
];
154 if(maxTime
<times
[i
]) maxTime
=times
[i
];
159 /* caculate standard deviation */
161 for(int i
=0;i
<n
;i
++) {
163 printf(" %d: %.9f\n", i
, times
[i
]);
165 sd
+= (times
[i
]-meanTime
)*(times
[i
]-meanTime
);
170 printf("sd: %.9f, mean: %.9f\n", sd
, meanTime
);
171 printf("min: %.9f, q1=%.9f, q2=%.9f, q3=%.9f, max=%.9f, n=%d\n", minTime
,q1
,q2
,q3
,maxTime
, n
);
172 printf("iqr/sd = %.9f\n", iqr
/sd
);
175 /* 1.960 = z sub 0.025 */
176 *marginOfError
= 1.960 * (sd
/sqrt(n
));
177 /*printf("Margin of Error = %.4f (95%% confidence)\n", me);*/
182 UBool calcSieveTime
= FALSE
;
183 double meanSieveTime
= 0.0;
184 double meanSieveME
= 0.0;
186 U_CAPI
double uprv_getSieveTime(double *marginOfError
) {
187 if(calcSieveTime
==FALSE
) {
189 double times
[SAMPLES
];
191 for(int i
=0;i
<SAMPLES
;i
++) {
192 times
[i
] = uprv_calcSieveTime();
194 printf("#%d/%d: %.9f\n", i
,SAMPLES
, times
[i
]);
198 meanSieveTime
= uprv_getMeanTime(times
, SAMPLES
,&meanSieveME
);
201 if(marginOfError
!=NULL
) {
202 *marginOfError
= meanSieveME
;
204 return meanSieveTime
;