2 ************************************************************************
3 * Copyright (c) 1997-2006, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 ************************************************************************
11 #include "unicode/utypes.h"
13 #if defined(U_WINDOWS)
15 # define WIN32_LEAN_AND_MEAN
19 # include <sys/time.h>
24 * This API provides functions for performing performance measurement
25 * There are 3 main usage scenarios.
26 * i) Loop until a threshold time is reached:
29 * typedef Params Params;
33 * const UChar* source;
35 * UNormalizationMode mode;
37 * void NormFn( void* param){
38 * Params* parameters = ( Params*) param;
39 * UErrorCode error = U_ZERO_ERROR;
40 * unorm_normalize(parameters->source, parameters->sourceLen, parameters->mode, 0, parameters->target, parameters->targetLen, &error);
41 * if(U_FAILURE(error)){
42 * printf("Normalization failed\n");
47 * // time the normalization function
48 * double timeTaken = 0;
50 * param.source // set up the source buffer
51 * param.target // set up the target buffer
54 * // time the loop for 10 seconds at least and find out the loop count and time taken
55 * timeTaken = utimer_loopUntilDone((double)10,(void*) param, NormFn, &loopCount);
59 * ii) Measure the time taken
62 * double perfNormalization(NormFn fn,const char* mode,Line* fileLines,int32_t loopCount){
65 * UErrorCode error = U_ZERO_ERROR;
67 * int32_t destCapacity=0;
69 * double elapsedTime = 0;
74 * destCapacity = 5000;
77 * // Initialize cache and ensure the data is loaded.
78 * // This loop checks for errors in Normalization. Once we pass the initialization
79 * // without errors we can safelly assume that there are no errors while timing the
81 * for (loops=0; loops<10; loops++) {
82 * for (line=0; line < gNumFileLines; line++) {
84 * len = fileLines[line].len;
87 * retVal= fn(fileLines[line].name,len,dest,destCapacity,&error);
88 * #if defined(U_WINDOWS)
90 * fprintf(stderr,"Normalization of string in Windows API failed for mode %s. ErrorNo: %i at line number %i\n",mode,GetLastError(),line);
94 * if(U_FAILURE(error)){
95 * fprintf(stderr,"Normalization of string in ICU API failed for mode %s. Error: %s at line number %i\n",mode,u_errorName(error),line);
104 * utimer_getTime(&start);
105 * for (loops=0; loops<loopCount; loops++) {
106 * for (line=0; line < gNumFileLines; line++) {
108 * len = fileLines[line].len;
111 * retVal= fn(fileLines[line].name,len,dest,destCapacity,&error);
116 * return utimer_getElapsedSeconds(&start);
120 * iii) Let a higher level function do the calculation of confidence levels etc.
123 * void perf(UTimer* timer, UChar* source, int32_t sourceLen, UChar* target, int32_t targetLen, int32_t loopCount,UNormalizationMode mode, UErrorCode* error){
125 * for (loops=0; loops<loopCount; loops++) {
126 * unorm_normalize(source,sourceLen,target, targetLen,mode,error);
128 * utimer_getTime(timer);
130 * void main(const char* argsc, int argv){
131 * // read the file and setup the data
133 * UTimer start,timer1, timer2, timer3, timer4;
134 * double NFDTimeTaken, NFCTimeTaken, FCDTimeTaken;
137 * utimer_getTime(start);
138 * perf(timer1, source,sourceLen, target, targetLen,loopCount,UNORM_NFD,&error);
139 * NFDTimeTaken = utimer_getDeltaSeconds(start,timer1);
141 * timer_getTime(start);
142 * perf(timer2,source,sourceLen,target,targetLen,loopCount,UNORM_NFC,&error);
143 * NFCTimeTaken = utimer_getDeltaSeconds(start,timer2);
144 * perf(timer3, source, sourceLen, target,targetLen, loopCount, UNORM_FCD,&error);
145 * // ........so on .............
147 * // calculate confidence levels etc and print
155 typedef struct UTimer UTimer
;
157 typedef void FuntionToBeTimed(void* param
);
160 #if defined(U_WINDOWS)
164 LARGE_INTEGER placeHolder
;
167 int uprv_initFrequency(UTimer
* timer
)
169 return QueryPerformanceFrequency(&timer
->placeHolder
);
171 void uprv_start(UTimer
* timer
)
173 QueryPerformanceCounter(&timer
->start
);
175 double uprv_delta(UTimer
* timer1
, UTimer
* timer2
){
176 return ((double)(timer2
->start
.QuadPart
- timer1
->start
.QuadPart
))/((double)timer1
->placeHolder
.QuadPart
);
178 UBool
uprv_compareFrequency(UTimer
* timer1
, UTimer
* timer2
){
179 return (timer1
->placeHolder
.QuadPart
== timer2
->placeHolder
.QuadPart
);
185 struct timeval start
;
186 struct timeval placeHolder
;
189 int32_t uprv_initFrequency(UTimer
* /*timer*/)
193 void uprv_start(UTimer
* timer
)
195 gettimeofday(&timer
->start
, 0);
197 double uprv_delta(UTimer
* timer1
, UTimer
* timer2
){
200 t1
= (double)timer1
->start
.tv_sec
+ (double)timer1
->start
.tv_usec
/(1000*1000);
201 t2
= (double)timer2
->start
.tv_sec
+ (double)timer2
->start
.tv_usec
/(1000*1000);
204 UBool
uprv_compareFrequency(UTimer
* /*timer1*/, UTimer
* /*timer2*/){
210 * Intializes the timer with the current time
212 * @param timer A pointer to UTimer struct to recieve the current time
214 static U_INLINE
void U_EXPORT2
215 utimer_getTime(UTimer
* timer
){
216 uprv_initFrequency(timer
);
221 * Returns the difference in times between timer1 and timer2 by subtracting
222 * timer1's time from timer2's time
224 * @param timer1 A pointer to UTimer struct to be used as starting time
225 * @param timer2 A pointer to UTimer struct to be used as end time
226 * @return Time in seconds
228 static U_INLINE
double U_EXPORT2
229 utimer_getDeltaSeconds(UTimer
* timer1
, UTimer
* timer2
){
230 if(uprv_compareFrequency(timer1
,timer2
)){
231 return uprv_delta(timer1
,timer2
);
233 /* got error return -1 */
238 * Returns the time elapsed from the starting time represented by the
239 * UTimer struct pointer passed
240 * @param timer A pointer to UTimer struct to be used as starting time
241 * @return Time elapsed in seconds
243 static U_INLINE
double U_EXPORT2
244 utimer_getElapsedSeconds(UTimer
* timer
){
246 utimer_getTime(&temp
);
247 return uprv_delta(timer
,&temp
);
251 * Executes the function pointed to for a given time and returns exact time
252 * taken and number of iterations of the loop
253 * @param thresholTimeVal
254 * @param loopCount output param to recieve the number of iterations
255 * @param fn The funtion to be executed
256 * @param param Parameters to be passed to the fn
257 * @return the time elapsed in seconds
259 static U_INLINE
double U_EXPORT2
260 utimer_loopUntilDone(double thresholdTimeVal
,
267 utimer_getTime(&timer
);
268 for(;currentVal
<thresholdTimeVal
;){
270 currentVal
= utimer_getElapsedSeconds(&timer
);