2 ************************************************************************
3 * Copyright (c) 1997-2004, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 ************************************************************************
11 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
13 # define WIN32_LEAN_AND_MEAN
17 # include <sys/time.h>
22 * This API provides functions for performing performance measurement
23 * There are 3 main usage scenarios.
24 * i) Loop until a threshold time is reached:
27 * typedef Params Params;
31 * const UChar* source;
33 * UNormalizationMode mode;
35 * void NormFn( void* param){
36 * Params* parameters = ( Params*) param;
37 * UErrorCode error = U_ZERO_ERROR;
38 * unorm_normalize(parameters->source, parameters->sourceLen, parameters->mode, 0, parameters->target, parameters->targetLen, &error);
39 * if(U_FAILURE(error)){
40 * printf("Normalization failed\n");
45 * // time the normalization function
46 * double timeTaken = 0;
48 * param.source // set up the source buffer
49 * param.target // set up the target buffer
52 * // time the loop for 10 seconds at least and find out the loop count and time taken
53 * timeTaken = utimer_loopUntilDone((double)10,(void*) param, NormFn, &loopCount);
57 * ii) Measure the time taken
60 * double perfNormalization(NormFn fn,const char* mode,Line* fileLines,int32_t loopCount){
63 * UErrorCode error = U_ZERO_ERROR;
65 * int32_t destCapacity=0;
67 * double elapsedTime = 0;
72 * destCapacity = 5000;
75 * // Initialize cache and ensure the data is loaded.
76 * // This loop checks for errors in Normalization. Once we pass the initialization
77 * // without errors we can safelly assume that there are no errors while timing the
79 * for (loops=0; loops<10; loops++) {
80 * for (line=0; line < gNumFileLines; line++) {
82 * len = fileLines[line].len;
85 * retVal= fn(fileLines[line].name,len,dest,destCapacity,&error);
86 * #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
88 * fprintf(stderr,"Normalization of string in Windows API failed for mode %s. ErrorNo: %i at line number %i\n",mode,GetLastError(),line);
92 * if(U_FAILURE(error)){
93 * fprintf(stderr,"Normalization of string in ICU API failed for mode %s. Error: %s at line number %i\n",mode,u_errorName(error),line);
102 * utimer_getTime(&start);
103 * for (loops=0; loops<loopCount; loops++) {
104 * for (line=0; line < gNumFileLines; line++) {
106 * len = fileLines[line].len;
109 * retVal= fn(fileLines[line].name,len,dest,destCapacity,&error);
114 * return utimer_getElapsedSeconds(&start);
118 * iii) Let a higher level function do the calculation of confidence levels etc.
121 * void perf(UTimer* timer, UChar* source, int32_t sourceLen, UChar* target, int32_t targetLen, int32_t loopCount,UNormalizationMode mode, UErrorCode* error){
123 * for (loops=0; loops<loopCount; loops++) {
124 * unorm_normalize(source,sourceLen,target, targetLen,mode,error);
126 * utimer_getTime(timer);
128 * void main(const char* argsc, int argv){
129 * // read the file and setup the data
131 * UTimer start,timer1, timer2, timer3, timer4;
132 * double NFDTimeTaken, NFCTimeTaken, FCDTimeTaken;
135 * utimer_getTime(start);
136 * perf(timer1, source,sourceLen, target, targetLen,loopCount,UNORM_NFD,&error);
137 * NFDTimeTaken = utimer_getDeltaSeconds(start,timer1);
139 * timer_getTime(start);
140 * perf(timer2,source,sourceLen,target,targetLen,loopCount,UNORM_NFC,&error);
141 * NFCTimeTaken = utimer_getDeltaSeconds(start,timer2);
142 * perf(timer3, source, sourceLen, target,targetLen, loopCount, UNORM_FCD,&error);
143 * // ........so on .............
145 * // calculate confidence levels etc and print
153 typedef struct UTimer UTimer
;
155 typedef void FuntionToBeTimed(void* param
);
158 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
162 LARGE_INTEGER placeHolder
;
165 int uprv_initFrequency(UTimer
* timer
)
167 return QueryPerformanceFrequency(&timer
->placeHolder
);
169 void uprv_start(UTimer
* timer
)
171 QueryPerformanceCounter(&timer
->start
);
173 double uprv_delta(UTimer
* timer1
, UTimer
* timer2
){
174 return ((double)(timer2
->start
.QuadPart
- timer1
->start
.QuadPart
))/((double)timer1
->placeHolder
.QuadPart
);
176 UBool
uprv_compareFrequency(UTimer
* timer1
, UTimer
* timer2
){
177 return (timer1
->placeHolder
.QuadPart
== timer2
->placeHolder
.QuadPart
);
183 struct timeval start
;
184 struct timeval placeHolder
;
187 int32_t uprv_initFrequency(UTimer
* /*timer*/)
191 void uprv_start(UTimer
* timer
)
193 gettimeofday(&timer
->start
, 0);
195 double uprv_delta(UTimer
* timer1
, UTimer
* timer2
){
198 t1
= (double)timer1
->start
.tv_sec
+ (double)timer1
->start
.tv_usec
/(1000*1000);
199 t2
= (double)timer2
->start
.tv_sec
+ (double)timer2
->start
.tv_usec
/(1000*1000);
202 UBool
uprv_compareFrequency(UTimer
* /*timer1*/, UTimer
* /*timer2*/){
208 * Intializes the timer with the current time
210 * @param timer A pointer to UTimer struct to recieve the current time
212 U_CAPI
void U_EXPORT2
213 utimer_getTime(UTimer
* timer
){
214 uprv_initFrequency(timer
);
219 * Returns the difference in times between timer1 and timer2 by subtracting
220 * timer1's time from timer2's time
222 * @param timer1 A pointer to UTimer struct to be used as starting time
223 * @param timer2 A pointer to UTimer struct to be used as end time
224 * @return Time in seconds
226 U_CAPI
double U_EXPORT2
227 utimer_getDeltaSeconds(UTimer
* timer1
, UTimer
* timer2
){
228 if(uprv_compareFrequency(timer1
,timer2
)){
229 return uprv_delta(timer1
,timer2
);
231 /* got error return -1 */
236 * Returns the time elapsed from the starting time represented by the
237 * UTimer struct pointer passed
238 * @param timer A pointer to UTimer struct to be used as starting time
239 * @return Time elapsed in seconds
241 U_CAPI
double U_EXPORT2
242 utimer_getElapsedSeconds(UTimer
* timer
){
244 utimer_getTime(&temp
);
245 return uprv_delta(timer
,&temp
);
249 * Executes the function pointed to for a given time and returns exact time
250 * taken and number of iterations of the loop
251 * @param thresholTimeVal
252 * @param loopCount output param to recieve the number of iterations
253 * @param fn The funtion to be executed
254 * @param param Parameters to be passed to the fn
255 * @return the time elapsed in seconds
257 U_CAPI
double U_EXPORT2
258 utimer_loopUntilDone(double thresholdTimeVal
,
265 utimer_getTime(&timer
);
266 for(;currentVal
<thresholdTimeVal
;){
268 currentVal
= utimer_getElapsedSeconds(&timer
);