2 **********************************************************************
3 * Copyright (c) 2002-2004, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 **********************************************************************
6 **********************************************************************
14 #include "unicode/utypes.h"
15 #include "unicode/unistr.h"
17 typedef std::wstring stlstring
;
19 /* Define all constants for test case operations */
20 #define MAXNUMLINES 40000 //Max number of lines in a test data file
21 #define MAXSRCLEN 20 //Max length of one line. maybe a larger number, but it need more mem
22 #define LOOPS 100 //Iterations
24 #define catenate_STRLEN 2
26 const UChar uTESTCHAR1
= 'a';
27 const wchar_t wTESTCHAR1
= 'a';
28 const UnicodeString uEMPTY
;
29 const stlstring sEMPTY
;
32 // Simulate construction with a single-char string for basic_string
33 wchar_t simulate
[2]={wTESTCHAR1
, 0};
35 /* Constants for scan operation */
36 U_STRING_DECL(scan_STRING
, "Dot. 123. Some more data.", 25);
37 const UnicodeString uScan_STRING
=UnicodeString(scan_STRING
);
38 const stlstring sScan_STRING
=stlstring(L
"Dot. 123. Some more data.");
40 /* global variables or constants for concatenation operation */
41 U_STRING_DECL(uCatenate_STR
, "!!", 2);
42 const stlstring sCatenate_STR
=stlstring(L
"!!");
43 static UnicodeString
* catICU
;
44 static stlstring
* catStd
;
45 UBool bCatenatePrealloc
;
48 typedef struct WLine WLine
;
52 }; //struct to store one line of wchar_t string
54 enum FnType
{ Fn_ICU
, Fn_STD
};
55 typedef FnType FnType
;
56 typedef void (*ICUStringPerfFn
)(const UChar
* src
,int32_t srcLen
, UnicodeString s0
);
57 typedef void (*StdStringPerfFn
)(const wchar_t* src
,int32_t srcLen
, stlstring s0
);
60 class StringPerfFunction
: public UPerfFunction
64 virtual long getEventsPerIteration(){
66 if (catICU
) { delete catICU
;}
67 if (catStd
) { delete catStd
;}
69 if (bCatenatePrealloc
) {
71 int to_alloc
= loops
* MAXNUMLINES
* (MAXSRCLEN
+ catenate_STRLEN
);
72 catICU
= new UnicodeString(to_alloc
,'a',0);
73 //catICU = new UnicodeString();
75 catStd
= new stlstring();
76 //catStd -> reserve(loops * MAXNUMLINES * (MAXSRCLEN + catenate_STRLEN));
77 catStd
-> reserve(110000000);
79 catICU
= new UnicodeString();
80 catStd
= new stlstring();
86 virtual void call(UErrorCode
* status
)
90 for(int32_t i
= 0; i
< numLines_
; i
++){
91 if (fnType_
==Fn_ICU
) {
92 (*fn1_
)(lines_
[i
].name
,lines_
[i
].len
,uS0_
[i
]);
94 (*fn2_
)(wlines_
[i
].name
,wlines_
[i
].len
,sS0_
[i
]);
98 for(int32_t i
= 0; i
< numLines_
; i
++){
99 if (fnType_
==Fn_ICU
) {
100 (*fn1_
)(lines_
[i
].name
,-1,uS0_
[i
]);
102 (*fn2_
)(wlines_
[i
].name
,-1,sS0_
[i
]);
108 if (fnType_
==Fn_ICU
) {
109 (*fn1_
)(src_
,srcLen_
,*ubulk_
);
111 (*fn2_
)(wsrc_
,wsrcLen_
,*sbulk_
);
114 if (fnType_
==Fn_ICU
) {
115 (*fn1_
)(src_
,-1,*ubulk_
);
117 (*fn2_
)(wsrc_
,-1,*sbulk_
);
123 virtual long getOperationsPerIteration()
125 if(line_mode_
==TRUE
){
132 StringPerfFunction(ICUStringPerfFn func
, ULine
* srcLines
, int32_t srcNumLines
, UBool uselen
)
138 numLines_
=srcNumLines
;
147 uS0_
=new UnicodeString
[numLines_
];
148 for(int32_t i
=0; i
<numLines_
; i
++) {
149 uS0_
[i
]=UnicodeString(lines_
[i
].name
, lines_
[i
].len
);
156 StringPerfFunction(StdStringPerfFn func
, ULine
* srcLines
, int32_t srcNumLines
, UBool uselen
)
162 numLines_
=srcNumLines
;
175 //fillin wlines_[], sS0_[]
176 prepareLinesForStd();
179 StringPerfFunction(ICUStringPerfFn func
, UChar
* source
, int32_t sourceLen
, UBool uselen
)
188 src_
= new UChar
[sourceLen
];
189 memcpy(src_
, source
, sourceLen
* U_SIZEOF_UCHAR
);
197 ubulk_
=new UnicodeString(src_
,srcLen_
);
201 StringPerfFunction(StdStringPerfFn func
, UChar
* source
, int32_t sourceLen
, UBool uselen
)
210 src_
= new UChar
[sourceLen
];
211 memcpy(src_
, source
, sourceLen
* U_SIZEOF_UCHAR
);
219 //fillin wsrc_, sbulk_
224 ~StringPerfFunction()
237 void prepareLinesForStd(void)
239 UErrorCode err
=U_ZERO_ERROR
;
241 wlines_
=new WLine
[numLines_
];
243 int32_t wcap
= sizeof(ws
) / sizeof(*ws
);
247 sS0_
=new stlstring
[numLines_
];
248 for(int32_t i
=0; i
<numLines_
; i
++) {
250 wcs
= u_strToWCS(ws
, wcap
, &wl
, lines_
[i
].name
, lines_
[i
].len
, &err
);
251 memcpy(wlines_
[i
].name
, wcs
, wl
* sizeof(wchar_t));
253 sS0_
[i
]=stlstring(wlines_
[i
].name
, wlines_
[i
].len
);
255 wcs
= u_strToWCS(ws
, wcap
, &wl
, lines_
[i
].name
, lines_
[i
].len
-1, &err
);
256 memcpy(wlines_
[i
].name
, wcs
, wl
*sizeof(wchar_t));
258 sS0_
[i
]=stlstring(wlines_
[i
].name
, wlines_
[i
].len
+1);
261 if (U_FAILURE(err
)) {
268 void prepareBulkForStd(void)
270 UErrorCode err
=U_ZERO_ERROR
;
272 const UChar
* uSrc
= src_
;
273 int32_t uSrcLen
= srcLen_
;
274 wchar_t* wDest
= NULL
;
275 int32_t wDestLen
= 0;
280 u_strToWCS(wDest
,wDestLen
,&reqLen
,uSrc
,uSrcLen
,&err
);
282 if(err
== U_BUFFER_OVERFLOW_ERROR
){
284 wDest
=(wchar_t*) malloc(sizeof(wchar_t) * (reqLen
));
286 u_strToWCS(wDest
,wDestLen
,&reqLen
,uSrc
,uSrcLen
,&err
);
289 if (U_SUCCESS(err
)) {
292 sbulk_
=new stlstring(wsrc_
,wsrcLen_
);
297 u_strToWCS(wDest
,wDestLen
,&reqLen
,uSrc
,uSrcLen
-1,&err
);
299 if(err
== U_BUFFER_OVERFLOW_ERROR
){
301 wDest
=(wchar_t*) malloc(sizeof(wchar_t) * (reqLen
+1));
303 u_strToWCS(wDest
,wDestLen
,&reqLen
,uSrc
,uSrcLen
-1,&err
);
306 if (U_SUCCESS(err
)) {
309 sbulk_
=new stlstring(wsrc_
);
318 ICUStringPerfFn fn1_
;
319 StdStringPerfFn fn2_
;
332 //added for preparing testing data
335 UnicodeString
* ubulk_
;
341 class StringPerformanceTest
: public UPerfTest
344 StringPerformanceTest(int32_t argc
, const char *argv
[], UErrorCode
&status
);
345 ~StringPerformanceTest();
346 virtual UPerfFunction
* runIndexedTest(int32_t index
, UBool exec
,
349 UPerfFunction
* TestCtor();
350 UPerfFunction
* TestCtor1();
351 UPerfFunction
* TestCtor2();
352 UPerfFunction
* TestCtor3();
353 UPerfFunction
* TestAssign();
354 UPerfFunction
* TestAssign1();
355 UPerfFunction
* TestAssign2();
356 UPerfFunction
* TestGetch();
357 UPerfFunction
* TestCatenate();
358 UPerfFunction
* TestScan();
359 UPerfFunction
* TestScan1();
360 UPerfFunction
* TestScan2();
362 UPerfFunction
* TestStdLibCtor();
363 UPerfFunction
* TestStdLibCtor1();
364 UPerfFunction
* TestStdLibCtor2();
365 UPerfFunction
* TestStdLibCtor3();
366 UPerfFunction
* TestStdLibAssign();
367 UPerfFunction
* TestStdLibAssign1();
368 UPerfFunction
* TestStdLibAssign2();
369 UPerfFunction
* TestStdLibGetch();
370 UPerfFunction
* TestStdLibCatenate();
371 UPerfFunction
* TestStdLibScan();
372 UPerfFunction
* TestStdLibScan1();
373 UPerfFunction
* TestStdLibScan2();
379 int32_t StrBufferLen
;
384 inline void ctor(const UChar
* src
,int32_t srcLen
, UnicodeString s0
)
389 inline void ctor1(const UChar
* src
,int32_t srcLen
, UnicodeString s0
)
391 UnicodeString
b(uTESTCHAR1
);
394 inline void ctor2(const UChar
* src
,int32_t srcLen
, UnicodeString s0
)
396 UnicodeString
c(uEMPTY
);
399 inline void ctor3(const UChar
* src
,int32_t srcLen
, UnicodeString s0
)
401 UnicodeString
d(src
,srcLen
);
404 inline UnicodeString
icu_assign_helper(const UChar
* src
,int32_t srcLen
)
406 if (srcLen
==-1) { return src
;}
407 else { return UnicodeString(src
, srcLen
);}
410 inline void assign(const UChar
* src
,int32_t srcLen
, UnicodeString s0
)
412 unistr
= icu_assign_helper(src
,srcLen
);
415 inline void assign1(const UChar
* src
,int32_t srcLen
, UnicodeString s0
)
417 unistr
.setTo(src
, srcLen
);
420 inline void assign2(const UChar
* src
,int32_t srcLen
, UnicodeString s0
)
425 inline void getch(const UChar
* src
,int32_t srcLen
, UnicodeString s0
)
431 inline void catenate(const UChar
* src
,int32_t srcLen
, UnicodeString s0
)
433 UTimer mystart
, mystop
;
434 utimer_getTime(&mystart
);
438 utimer_getTime(&mystop
);
439 double mytime
= utimer_getDeltaSeconds(&mystart
,&mystop
);
440 printf("\nmytime=%f \n", mytime
);
442 *catICU
+= uCatenate_STR
;
445 volatile int scan_idx
;
446 U_STRING_DECL(SCAN1
, "123", 3);
448 inline void scan(const UChar
* src
,int32_t srcLen
, UnicodeString s0
)
451 scan_idx
= uScan_STRING
.indexOf(c
);
454 inline void scan1(const UChar
* src
,int32_t srcLen
, UnicodeString s0
)
456 scan_idx
= uScan_STRING
.indexOf(SCAN1
,3);
459 inline void scan2(const UChar
* src
,int32_t srcLen
, UnicodeString s0
)
463 scan_idx
= uScan_STRING
.indexOf(c1
);
464 scan_idx
= uScan_STRING
.indexOf(c2
);
468 inline void StdLibCtor(const wchar_t* src
,int32_t srcLen
, stlstring s0
)
473 inline void StdLibCtor1(const wchar_t* src
,int32_t srcLen
, stlstring s0
)
476 stlstring
b(simulate
);
479 inline void StdLibCtor2(const wchar_t* src
,int32_t srcLen
, stlstring s0
)
484 inline void StdLibCtor3(const wchar_t* src
,int32_t srcLen
, stlstring s0
)
489 stlstring
d(src
, srcLen
);
493 inline stlstring
stl_assign_helper(const wchar_t* src
,int32_t srcLen
)
495 if (srcLen
==-1) { return src
;}
496 else { return stlstring(src
, srcLen
);}
499 inline void StdLibAssign(const wchar_t* src
,int32_t srcLen
, stlstring s0
)
501 stlstr
= stl_assign_helper(src
,srcLen
);
504 inline void StdLibAssign1(const wchar_t* src
,int32_t srcLen
, stlstring s0
)
506 if (srcLen
==-1) { stlstr
=src
;}
507 else { stlstr
.assign(src
, srcLen
);}
510 inline void StdLibAssign2(const wchar_t* src
,int32_t srcLen
, stlstring s0
)
515 inline void StdLibGetch(const wchar_t* src
,int32_t srcLen
, stlstring s0
)
520 inline void StdLibCatenate(const wchar_t* src
,int32_t srcLen
, stlstring s0
)
522 UTimer mystart
, mystop
;
523 utimer_getTime(&mystart
);
526 *catStd
+= sCatenate_STR
;
528 utimer_getTime(&mystop
);
529 double mytime
= utimer_getDeltaSeconds(&mystart
,&mystop
);
530 printf("\nmytime=%f \n", mytime
);
534 inline void StdLibScan(const wchar_t* src
,int32_t srcLen
, stlstring s0
)
536 scan_idx
= (int) sScan_STRING
.find('.');
539 inline void StdLibScan1(const wchar_t* src
,int32_t srcLen
, stlstring s0
)
541 scan_idx
= (int) sScan_STRING
.find(L
"123");
544 inline void StdLibScan2(const wchar_t* src
,int32_t srcLen
, stlstring s0
)
546 scan_idx
= (int) sScan_STRING
.find_first_of(L
"sm");
549 #endif // STRINGPERF_H