michael@0: /* michael@0: ************************************************************************ michael@0: * Copyright (c) 1997-2012, International Business Machines michael@0: * Corporation and others. All Rights Reserved. michael@0: ************************************************************************ michael@0: */ michael@0: michael@0: #ifndef _UTIMER_H michael@0: #define _UTIMER_H michael@0: michael@0: #include "unicode/utypes.h" michael@0: michael@0: #if U_PLATFORM_HAS_WIN32_API michael@0: # define VC_EXTRALEAN michael@0: # define WIN32_LEAN_AND_MEAN michael@0: # include michael@0: #else michael@0: # if U_PLATFORM == U_PF_OS390 && !defined(__UU) michael@0: # define __UU /* Universal Unix - for struct timeval */ michael@0: # endif michael@0: # include michael@0: # include michael@0: # include michael@0: #endif michael@0: michael@0: /** michael@0: * This API provides functions for performing performance measurement michael@0: * There are 3 main usage scenarios. michael@0: * i) Loop until a threshold time is reached: michael@0: * Example: michael@0: * michael@0: * typedef Params Params; michael@0: * struct Params{ michael@0: * UChar* target; michael@0: * int32_t targetLen; michael@0: * const UChar* source; michael@0: * int32_t sourceLen; michael@0: * UNormalizationMode mode; michael@0: * } michael@0: * void NormFn( void* param){ michael@0: * Params* parameters = ( Params*) param; michael@0: * UErrorCode error = U_ZERO_ERROR; michael@0: * unorm_normalize(parameters->source, parameters->sourceLen, parameters->mode, 0, parameters->target, parameters->targetLen, &error); michael@0: * if(U_FAILURE(error)){ michael@0: * printf("Normalization failed\n"); michael@0: * } michael@0: * } michael@0: * michael@0: * int main(){ michael@0: * // time the normalization function michael@0: * double timeTaken = 0; michael@0: * Params param; michael@0: * param.source // set up the source buffer michael@0: * param.target // set up the target buffer michael@0: * .... so on ... michael@0: * UTimer timer; michael@0: * // time the loop for 10 seconds at least and find out the loop count and time taken michael@0: * timeTaken = utimer_loopUntilDone((double)10,(void*) param, NormFn, &loopCount); michael@0: * } michael@0: * michael@0: * michael@0: * ii) Measure the time taken michael@0: * Example: michael@0: * michael@0: * double perfNormalization(NormFn fn,const char* mode,Line* fileLines,int32_t loopCount){ michael@0: * int line; michael@0: * int loops; michael@0: * UErrorCode error = U_ZERO_ERROR; michael@0: * UChar* dest=NULL; michael@0: * int32_t destCapacity=0; michael@0: * int len =-1; michael@0: * double elapsedTime = 0; michael@0: * int retVal=0; michael@0: * michael@0: * UChar arr[5000]; michael@0: * dest=arr; michael@0: * destCapacity = 5000; michael@0: * UTimer start; michael@0: * michael@0: * // Initialize cache and ensure the data is loaded. michael@0: * // This loop checks for errors in Normalization. Once we pass the initialization michael@0: * // without errors we can safelly assume that there are no errors while timing the michael@0: * // funtion michael@0: * for (loops=0; loops<10; loops++) { michael@0: * for (line=0; line < gNumFileLines; line++) { michael@0: * if (opt_uselen) { michael@0: * len = fileLines[line].len; michael@0: * } michael@0: * michael@0: * retVal= fn(fileLines[line].name,len,dest,destCapacity,&error); michael@0: * #if U_PLATFORM_HAS_WIN32_API michael@0: * if(retVal==0 ){ michael@0: * fprintf(stderr,"Normalization of string in Windows API failed for mode %s. ErrorNo: %i at line number %i\n",mode,GetLastError(),line); michael@0: * return 0; michael@0: * } michael@0: * #endif michael@0: * if(U_FAILURE(error)){ michael@0: * fprintf(stderr,"Normalization of string in ICU API failed for mode %s. Error: %s at line number %i\n",mode,u_errorName(error),line); michael@0: * return 0; michael@0: * } michael@0: * michael@0: * } michael@0: * } michael@0: * michael@0: * //compute the time michael@0: * michael@0: * utimer_getTime(&start); michael@0: * for (loops=0; loops michael@0: * michael@0: * iii) Let a higher level function do the calculation of confidence levels etc. michael@0: * Example: michael@0: * michael@0: * void perf(UTimer* timer, UChar* source, int32_t sourceLen, UChar* target, int32_t targetLen, int32_t loopCount,UNormalizationMode mode, UErrorCode* error){ michael@0: * int32_t loops; michael@0: * for (loops=0; loops michael@0: * michael@0: */ michael@0: michael@0: typedef struct UTimer UTimer; michael@0: michael@0: typedef void FuntionToBeTimed(void* param); michael@0: michael@0: michael@0: #if U_PLATFORM_HAS_WIN32_API michael@0: michael@0: struct UTimer{ michael@0: LARGE_INTEGER start; michael@0: LARGE_INTEGER placeHolder; michael@0: }; michael@0: michael@0: static int uprv_initFrequency(UTimer* timer) michael@0: { michael@0: return QueryPerformanceFrequency(&timer->placeHolder); michael@0: } michael@0: static void uprv_start(UTimer* timer) michael@0: { michael@0: QueryPerformanceCounter(&timer->start); michael@0: } michael@0: static double uprv_delta(UTimer* timer1, UTimer* timer2){ michael@0: return ((double)(timer2->start.QuadPart - timer1->start.QuadPart))/((double)timer1->placeHolder.QuadPart); michael@0: } michael@0: static UBool uprv_compareFrequency(UTimer* timer1, UTimer* timer2){ michael@0: return (timer1->placeHolder.QuadPart == timer2->placeHolder.QuadPart); michael@0: } michael@0: michael@0: #else michael@0: michael@0: struct UTimer{ michael@0: struct timeval start; michael@0: struct timeval placeHolder; michael@0: }; michael@0: michael@0: static int32_t uprv_initFrequency(UTimer* /*timer*/) michael@0: { michael@0: return 0; michael@0: } michael@0: static void uprv_start(UTimer* timer) michael@0: { michael@0: gettimeofday(&timer->start, 0); michael@0: } michael@0: static double uprv_delta(UTimer* timer1, UTimer* timer2){ michael@0: double t1, t2; michael@0: michael@0: t1 = (double)timer1->start.tv_sec + (double)timer1->start.tv_usec/(1000*1000); michael@0: t2 = (double)timer2->start.tv_sec + (double)timer2->start.tv_usec/(1000*1000); michael@0: return (t2-t1); michael@0: } michael@0: static UBool uprv_compareFrequency(UTimer* /*timer1*/, UTimer* /*timer2*/){ michael@0: return TRUE; michael@0: } michael@0: michael@0: #endif michael@0: /** michael@0: * Intializes the timer with the current time michael@0: * michael@0: * @param timer A pointer to UTimer struct to recieve the current time michael@0: */ michael@0: static inline void U_EXPORT2 michael@0: utimer_getTime(UTimer* timer){ michael@0: uprv_initFrequency(timer); michael@0: uprv_start(timer); michael@0: } michael@0: michael@0: /** michael@0: * Returns the difference in times between timer1 and timer2 by subtracting michael@0: * timer1's time from timer2's time michael@0: * michael@0: * @param timer1 A pointer to UTimer struct to be used as starting time michael@0: * @param timer2 A pointer to UTimer struct to be used as end time michael@0: * @return Time in seconds michael@0: */ michael@0: static inline double U_EXPORT2 michael@0: utimer_getDeltaSeconds(UTimer* timer1, UTimer* timer2){ michael@0: if(uprv_compareFrequency(timer1,timer2)){ michael@0: return uprv_delta(timer1,timer2); michael@0: } michael@0: /* got error return -1 */ michael@0: return -1; michael@0: } michael@0: michael@0: /** michael@0: * Returns the time elapsed from the starting time represented by the michael@0: * UTimer struct pointer passed michael@0: * @param timer A pointer to UTimer struct to be used as starting time michael@0: * @return Time elapsed in seconds michael@0: */ michael@0: static inline double U_EXPORT2 michael@0: utimer_getElapsedSeconds(UTimer* timer){ michael@0: UTimer temp; michael@0: utimer_getTime(&temp); michael@0: return uprv_delta(timer,&temp); michael@0: } michael@0: michael@0: /** michael@0: * Executes the function pointed to for a given time and returns exact time michael@0: * taken and number of iterations of the loop michael@0: * @param thresholTimeVal michael@0: * @param loopCount output param to recieve the number of iterations michael@0: * @param fn The funtion to be executed michael@0: * @param param Parameters to be passed to the fn michael@0: * @return the time elapsed in seconds michael@0: */ michael@0: static inline double U_EXPORT2 michael@0: utimer_loopUntilDone(double thresholdTimeVal, michael@0: int32_t* loopCount, michael@0: FuntionToBeTimed fn, michael@0: void* param){ michael@0: UTimer timer; michael@0: double currentVal=0; michael@0: *loopCount = 0; michael@0: utimer_getTime(&timer); michael@0: for(;currentVal