2 **********************************************************************
3 * Copyright (c) 2002-2011, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 **********************************************************************
10 #include "unicode/utypes.h"
11 #include "unicode/unistr.h"
13 #include "unicode/uperf.h"
19 typedef std::wstring stlstring;
21 /* Define all constants for test case operations */
22 #define MAXNUMLINES 40000 //Max number of lines in a test data file
23 #define MAXSRCLEN 20 //Max length of one line. maybe a larger number, but it need more mem
24 #define LOOPS 100 //Iterations
26 #define catenate_STRLEN 2
28 const UChar uTESTCHAR1 = 'a';
29 const wchar_t wTESTCHAR1 = 'a';
30 const UnicodeString uEMPTY;
31 const stlstring sEMPTY;
34 // Simulate construction with a single-char string for basic_string
35 wchar_t simulate[2]={wTESTCHAR1, 0};
37 /* Constants for scan operation */
38 U_STRING_DECL(scan_STRING, "Dot. 123. Some more data.", 25);
39 const UnicodeString uScan_STRING=UnicodeString(scan_STRING);
40 const stlstring sScan_STRING=stlstring(L"Dot. 123. Some more data.");
42 /* global variables or constants for concatenation operation */
43 U_STRING_DECL(uCatenate_STR, "!!", 2);
44 const stlstring sCatenate_STR=stlstring(L"!!");
45 static UnicodeString* catICU;
46 static stlstring* catStd;
47 UBool bCatenatePrealloc;
50 typedef struct WLine WLine;
54 }; //struct to store one line of wchar_t string
56 enum FnType { Fn_ICU, Fn_STD };
57 typedef FnType FnType;
58 typedef void (*ICUStringPerfFn)(const UChar* src,int32_t srcLen, UnicodeString s0);
59 typedef void (*StdStringPerfFn)(const wchar_t* src,int32_t srcLen, stlstring s0);
62 class StringPerfFunction : public UPerfFunction
66 virtual long getEventsPerIteration(){
68 if (catICU) { delete catICU;}
69 if (catStd) { delete catStd;}
71 if (bCatenatePrealloc) {
73 int to_alloc = loops * MAXNUMLINES * (MAXSRCLEN + catenate_STRLEN);
74 catICU = new UnicodeString(to_alloc,'a',0);
75 //catICU = new UnicodeString();
77 catStd = new stlstring();
78 //catStd -> reserve(loops * MAXNUMLINES * (MAXSRCLEN + catenate_STRLEN));
79 catStd -> reserve(110000000);
81 catICU = new UnicodeString();
82 catStd = new stlstring();
88 virtual void call(UErrorCode* status)
92 for(int32_t i = 0; i< numLines_; i++){
93 if (fnType_==Fn_ICU) {
94 (*fn1_)(lines_[i].name,lines_[i].len,uS0_[i]);
96 (*fn2_)(wlines_[i].name,wlines_[i].len,sS0_[i]);
100 for(int32_t i = 0; i< numLines_; i++){
101 if (fnType_==Fn_ICU) {
102 (*fn1_)(lines_[i].name,-1,uS0_[i]);
104 (*fn2_)(wlines_[i].name,-1,sS0_[i]);
110 if (fnType_==Fn_ICU) {
111 (*fn1_)(src_,srcLen_,*ubulk_);
113 (*fn2_)(wsrc_,wsrcLen_,*sbulk_);
116 if (fnType_==Fn_ICU) {
117 (*fn1_)(src_,-1,*ubulk_);
119 (*fn2_)(wsrc_,-1,*sbulk_);
125 virtual long getOperationsPerIteration()
127 if(line_mode_==TRUE){
134 StringPerfFunction(ICUStringPerfFn func, ULine* srcLines, int32_t srcNumLines, UBool uselen)
140 numLines_=srcNumLines;
149 uS0_=new UnicodeString[numLines_];
150 for(int32_t i=0; i<numLines_; i++) {
151 uS0_[i]=UnicodeString(lines_[i].name, lines_[i].len);
158 StringPerfFunction(StdStringPerfFn func, ULine* srcLines, int32_t srcNumLines, UBool uselen)
164 numLines_=srcNumLines;
177 //fillin wlines_[], sS0_[]
178 prepareLinesForStd();
181 StringPerfFunction(ICUStringPerfFn func, UChar* source, int32_t sourceLen, UBool uselen)
190 src_ = new UChar[sourceLen];
191 memcpy(src_, source, sourceLen * U_SIZEOF_UCHAR);
199 ubulk_=new UnicodeString(src_,srcLen_);
203 StringPerfFunction(StdStringPerfFn func, UChar* source, int32_t sourceLen, UBool uselen)
212 src_ = new UChar[sourceLen];
213 memcpy(src_, source, sourceLen * U_SIZEOF_UCHAR);
221 //fillin wsrc_, sbulk_
226 ~StringPerfFunction()
239 void prepareLinesForStd(void)
241 UErrorCode err=U_ZERO_ERROR;
243 wlines_=new WLine[numLines_];
245 int32_t wcap = sizeof(ws) / sizeof(*ws);
249 sS0_=new stlstring[numLines_];
250 for(int32_t i=0; i<numLines_; i++) {
252 wcs = u_strToWCS(ws, wcap, &wl, lines_[i].name, lines_[i].len, &err);
253 memcpy(wlines_[i].name, wcs, wl * sizeof(wchar_t));
255 sS0_[i]=stlstring(wlines_[i].name, wlines_[i].len);
257 wcs = u_strToWCS(ws, wcap, &wl, lines_[i].name, lines_[i].len-1, &err);
258 memcpy(wlines_[i].name, wcs, wl*sizeof(wchar_t));
260 sS0_[i]=stlstring(wlines_[i].name, wlines_[i].len+1);
263 if (U_FAILURE(err)) {
270 void prepareBulkForStd(void)
272 UErrorCode err=U_ZERO_ERROR;
274 const UChar* uSrc = src_;
275 int32_t uSrcLen = srcLen_;
276 wchar_t* wDest = NULL;
277 int32_t wDestLen = 0;
282 u_strToWCS(wDest,wDestLen,&reqLen,uSrc,uSrcLen,&err);
284 if(err == U_BUFFER_OVERFLOW_ERROR){
286 wDest =(wchar_t*) malloc(sizeof(wchar_t) * (reqLen));
288 u_strToWCS(wDest,wDestLen,&reqLen,uSrc,uSrcLen,&err);
291 if (U_SUCCESS(err)) {
294 sbulk_=new stlstring(wsrc_,wsrcLen_);
299 u_strToWCS(wDest,wDestLen,&reqLen,uSrc,uSrcLen-1,&err);
301 if(err == U_BUFFER_OVERFLOW_ERROR){
303 wDest =(wchar_t*) malloc(sizeof(wchar_t) * (reqLen+1));
305 u_strToWCS(wDest,wDestLen,&reqLen,uSrc,uSrcLen-1,&err);
308 if (U_SUCCESS(err)) {
311 sbulk_=new stlstring(wsrc_);
320 ICUStringPerfFn fn1_;
321 StdStringPerfFn fn2_;
334 //added for preparing testing data
337 UnicodeString* ubulk_;
343 class StringPerformanceTest : public UPerfTest
346 StringPerformanceTest(int32_t argc, const char *argv[], UErrorCode &status);
347 ~StringPerformanceTest();
348 virtual UPerfFunction* runIndexedTest(int32_t index, UBool exec,
351 UPerfFunction* TestCtor();
352 UPerfFunction* TestCtor1();
353 UPerfFunction* TestCtor2();
354 UPerfFunction* TestCtor3();
355 UPerfFunction* TestAssign();
356 UPerfFunction* TestAssign1();
357 UPerfFunction* TestAssign2();
358 UPerfFunction* TestGetch();
359 UPerfFunction* TestCatenate();
360 UPerfFunction* TestScan();
361 UPerfFunction* TestScan1();
362 UPerfFunction* TestScan2();
364 UPerfFunction* TestStdLibCtor();
365 UPerfFunction* TestStdLibCtor1();
366 UPerfFunction* TestStdLibCtor2();
367 UPerfFunction* TestStdLibCtor3();
368 UPerfFunction* TestStdLibAssign();
369 UPerfFunction* TestStdLibAssign1();
370 UPerfFunction* TestStdLibAssign2();
371 UPerfFunction* TestStdLibGetch();
372 UPerfFunction* TestStdLibCatenate();
373 UPerfFunction* TestStdLibScan();
374 UPerfFunction* TestStdLibScan1();
375 UPerfFunction* TestStdLibScan2();
381 int32_t StrBufferLen;
386 inline void ctor(const UChar* src,int32_t srcLen, UnicodeString s0)
391 inline void ctor1(const UChar* src,int32_t srcLen, UnicodeString s0)
393 UnicodeString b(uTESTCHAR1);
396 inline void ctor2(const UChar* src,int32_t srcLen, UnicodeString s0)
398 UnicodeString c(uEMPTY);
401 inline void ctor3(const UChar* src,int32_t srcLen, UnicodeString s0)
403 UnicodeString d(src,srcLen);
406 inline UnicodeString icu_assign_helper(const UChar* src,int32_t srcLen)
408 if (srcLen==-1) { return src;}
409 else { return UnicodeString(src, srcLen);}
412 inline void assign(const UChar* src,int32_t srcLen, UnicodeString s0)
414 unistr = icu_assign_helper(src,srcLen);
417 inline void assign1(const UChar* src,int32_t srcLen, UnicodeString s0)
419 unistr.setTo(src, srcLen);
422 inline void assign2(const UChar* src,int32_t srcLen, UnicodeString s0)
427 inline void getch(const UChar* src,int32_t srcLen, UnicodeString s0)
433 inline void catenate(const UChar* src,int32_t srcLen, UnicodeString s0)
435 UTimer mystart, mystop;
436 utimer_getTime(&mystart);
440 utimer_getTime(&mystop);
441 double mytime = utimer_getDeltaSeconds(&mystart,&mystop);
442 printf("\nmytime=%f \n", mytime);
444 *catICU += uCatenate_STR;
447 volatile int scan_idx;
448 U_STRING_DECL(SCAN1, "123", 3);
450 inline void scan(const UChar* src,int32_t srcLen, UnicodeString s0)
453 scan_idx = uScan_STRING.indexOf(c);
456 inline void scan1(const UChar* src,int32_t srcLen, UnicodeString s0)
458 scan_idx = uScan_STRING.indexOf(SCAN1,3);
461 inline void scan2(const UChar* src,int32_t srcLen, UnicodeString s0)
465 scan_idx = uScan_STRING.indexOf(c1);
466 scan_idx = uScan_STRING.indexOf(c2);
470 inline void StdLibCtor(const wchar_t* src,int32_t srcLen, stlstring s0)
475 inline void StdLibCtor1(const wchar_t* src,int32_t srcLen, stlstring s0)
477 stlstring b(simulate);
480 inline void StdLibCtor2(const wchar_t* src,int32_t srcLen, stlstring s0)
485 inline void StdLibCtor3(const wchar_t* src,int32_t srcLen, stlstring s0)
490 stlstring d(src, srcLen);
494 inline stlstring stl_assign_helper(const wchar_t* src,int32_t srcLen)
496 if (srcLen==-1) { return src;}
497 else { return stlstring(src, srcLen);}
500 inline void StdLibAssign(const wchar_t* src,int32_t srcLen, stlstring s0)
502 stlstr = stl_assign_helper(src,srcLen);
505 inline void StdLibAssign1(const wchar_t* src,int32_t srcLen, stlstring s0)
507 if (srcLen==-1) { stlstr=src;}
508 else { stlstr.assign(src, srcLen);}
511 inline void StdLibAssign2(const wchar_t* src,int32_t srcLen, stlstring s0)
516 inline void StdLibGetch(const wchar_t* src,int32_t srcLen, stlstring s0)
521 inline void StdLibCatenate(const wchar_t* src,int32_t srcLen, stlstring s0)
523 UTimer mystart, mystop;
524 utimer_getTime(&mystart);
527 *catStd += sCatenate_STR;
529 utimer_getTime(&mystop);
530 double mytime = utimer_getDeltaSeconds(&mystart,&mystop);
531 printf("\nmytime=%f \n", mytime);
535 inline void StdLibScan(const wchar_t* src,int32_t srcLen, stlstring s0)
537 scan_idx = (int) sScan_STRING.find('.');
540 inline void StdLibScan1(const wchar_t* src,int32_t srcLen, stlstring s0)
542 scan_idx = (int) sScan_STRING.find(L"123");
545 inline void StdLibScan2(const wchar_t* src,int32_t srcLen, stlstring s0)
547 scan_idx = (int) sScan_STRING.find_first_of(L"sm");
550 #endif // STRINGPERF_H