2 ************************************************************************
3 * Copyright (c) 1997-2012, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 ************************************************************************
11 #include "unicode/utypes.h"
13 #if U_PLATFORM_HAS_WIN32_API
15 # define WIN32_LEAN_AND_MEAN
18 # if U_PLATFORM == U_PF_OS390 && !defined(__UU)
19 # define __UU /* Universal Unix - for struct timeval */
22 # include <sys/time.h>
27 * This API provides functions for performing performance measurement
28 * There are 3 main usage scenarios.
29 * i) Loop until a threshold time is reached:
32 * typedef Params Params;
36 * const UChar* source;
38 * UNormalizationMode mode;
40 * void NormFn( void* param){
41 * Params* parameters = ( Params*) param;
42 * UErrorCode error = U_ZERO_ERROR;
43 * unorm_normalize(parameters->source, parameters->sourceLen, parameters->mode, 0, parameters->target, parameters->targetLen, &error);
44 * if(U_FAILURE(error)){
45 * printf("Normalization failed\n");
50 * // time the normalization function
51 * double timeTaken = 0;
53 * param.source // set up the source buffer
54 * param.target // set up the target buffer
57 * // time the loop for 10 seconds at least and find out the loop count and time taken
58 * timeTaken = utimer_loopUntilDone((double)10,(void*) param, NormFn, &loopCount);
62 * ii) Measure the time taken
65 * double perfNormalization(NormFn fn,const char* mode,Line* fileLines,int32_t loopCount){
68 * UErrorCode error = U_ZERO_ERROR;
70 * int32_t destCapacity=0;
72 * double elapsedTime = 0;
77 * destCapacity = 5000;
80 * // Initialize cache and ensure the data is loaded.
81 * // This loop checks for errors in Normalization. Once we pass the initialization
82 * // without errors we can safelly assume that there are no errors while timing the
84 * for (loops=0; loops<10; loops++) {
85 * for (line=0; line < gNumFileLines; line++) {
87 * len = fileLines[line].len;
90 * retVal= fn(fileLines[line].name,len,dest,destCapacity,&error);
91 * #if U_PLATFORM_HAS_WIN32_API
93 * fprintf(stderr,"Normalization of string in Windows API failed for mode %s. ErrorNo: %i at line number %i\n",mode,GetLastError(),line);
97 * if(U_FAILURE(error)){
98 * fprintf(stderr,"Normalization of string in ICU API failed for mode %s. Error: %s at line number %i\n",mode,u_errorName(error),line);
107 * utimer_getTime(&start);
108 * for (loops=0; loops<loopCount; loops++) {
109 * for (line=0; line < gNumFileLines; line++) {
111 * len = fileLines[line].len;
114 * retVal= fn(fileLines[line].name,len,dest,destCapacity,&error);
119 * return utimer_getElapsedSeconds(&start);
123 * iii) Let a higher level function do the calculation of confidence levels etc.
126 * void perf(UTimer* timer, UChar* source, int32_t sourceLen, UChar* target, int32_t targetLen, int32_t loopCount,UNormalizationMode mode, UErrorCode* error){
128 * for (loops=0; loops<loopCount; loops++) {
129 * unorm_normalize(source,sourceLen,target, targetLen,mode,error);
131 * utimer_getTime(timer);
133 * void main(const char* argsc, int argv){
134 * // read the file and setup the data
136 * UTimer start,timer1, timer2, timer3, timer4;
137 * double NFDTimeTaken, NFCTimeTaken, FCDTimeTaken;
140 * utimer_getTime(start);
141 * perf(timer1, source,sourceLen, target, targetLen,loopCount,UNORM_NFD,&error);
142 * NFDTimeTaken = utimer_getDeltaSeconds(start,timer1);
144 * timer_getTime(start);
145 * perf(timer2,source,sourceLen,target,targetLen,loopCount,UNORM_NFC,&error);
146 * NFCTimeTaken = utimer_getDeltaSeconds(start,timer2);
147 * perf(timer3, source, sourceLen, target,targetLen, loopCount, UNORM_FCD,&error);
148 * // ........so on .............
150 * // calculate confidence levels etc and print
158 typedef struct UTimer UTimer;
160 typedef void FuntionToBeTimed(void* param);
163 #if U_PLATFORM_HAS_WIN32_API
167 LARGE_INTEGER placeHolder;
170 static int uprv_initFrequency(UTimer* timer)
172 return QueryPerformanceFrequency(&timer->placeHolder);
174 static void uprv_start(UTimer* timer)
176 QueryPerformanceCounter(&timer->start);
178 static double uprv_delta(UTimer* timer1, UTimer* timer2){
179 return ((double)(timer2->start.QuadPart - timer1->start.QuadPart))/((double)timer1->placeHolder.QuadPart);
181 static UBool uprv_compareFrequency(UTimer* timer1, UTimer* timer2){
182 return (timer1->placeHolder.QuadPart == timer2->placeHolder.QuadPart);
188 struct timeval start;
189 struct timeval placeHolder;
192 static int32_t uprv_initFrequency(UTimer* /*timer*/)
196 static void uprv_start(UTimer* timer)
198 gettimeofday(&timer->start, 0);
200 static double uprv_delta(UTimer* timer1, UTimer* timer2){
203 t1 = (double)timer1->start.tv_sec + (double)timer1->start.tv_usec/(1000*1000);
204 t2 = (double)timer2->start.tv_sec + (double)timer2->start.tv_usec/(1000*1000);
207 static UBool uprv_compareFrequency(UTimer* /*timer1*/, UTimer* /*timer2*/){
213 * Intializes the timer with the current time
215 * @param timer A pointer to UTimer struct to recieve the current time
217 static inline void U_EXPORT2
218 utimer_getTime(UTimer* timer){
219 uprv_initFrequency(timer);
224 * Returns the difference in times between timer1 and timer2 by subtracting
225 * timer1's time from timer2's time
227 * @param timer1 A pointer to UTimer struct to be used as starting time
228 * @param timer2 A pointer to UTimer struct to be used as end time
229 * @return Time in seconds
231 static inline double U_EXPORT2
232 utimer_getDeltaSeconds(UTimer* timer1, UTimer* timer2){
233 if(uprv_compareFrequency(timer1,timer2)){
234 return uprv_delta(timer1,timer2);
236 /* got error return -1 */
241 * Returns the time elapsed from the starting time represented by the
242 * UTimer struct pointer passed
243 * @param timer A pointer to UTimer struct to be used as starting time
244 * @return Time elapsed in seconds
246 static inline double U_EXPORT2
247 utimer_getElapsedSeconds(UTimer* timer){
249 utimer_getTime(&temp);
250 return uprv_delta(timer,&temp);
254 * Executes the function pointed to for a given time and returns exact time
255 * taken and number of iterations of the loop
256 * @param thresholTimeVal
257 * @param loopCount output param to recieve the number of iterations
258 * @param fn The funtion to be executed
259 * @param param Parameters to be passed to the fn
260 * @return the time elapsed in seconds
262 static inline double U_EXPORT2
263 utimer_loopUntilDone(double thresholdTimeVal,
270 utimer_getTime(&timer);
271 for(;currentVal<thresholdTimeVal;){
273 currentVal = utimer_getElapsedSeconds(&timer);