2 **********************************************************************
3 * Copyright (c) 2002-2016, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 **********************************************************************
11 #include "unicode/utypes.h"
12 #include "unicode/unistr.h"
14 #include "unicode/uperf.h"
20 typedef std::wstring stlstring
;
22 /* Define all constants for test case operations */
23 #define MAXNUMLINES 40000 //Max number of lines in a test data file
24 #define MAXSRCLEN 20 //Max length of one line. maybe a larger number, but it need more mem
25 #define LOOPS 100 //Iterations
27 #define catenate_STRLEN 2
29 const UChar uTESTCHAR1
= 'a';
30 const wchar_t wTESTCHAR1
= 'a';
31 const UnicodeString uEMPTY
;
32 const stlstring sEMPTY
;
35 // Simulate construction with a single-char string for basic_string
36 wchar_t simulate
[2]={wTESTCHAR1
, 0};
38 /* Constants for scan operation */
39 U_STRING_DECL(scan_STRING
, "Dot. 123. Some more data.", 25);
40 const UnicodeString uScan_STRING
=UnicodeString(scan_STRING
);
41 const stlstring sScan_STRING
=stlstring(L
"Dot. 123. Some more data.");
43 /* global variables or constants for concatenation operation */
44 U_STRING_DECL(uCatenate_STR
, "!!", 2);
45 const stlstring sCatenate_STR
=stlstring(L
"!!");
46 static UnicodeString
* catICU
;
47 static stlstring
* catStd
;
48 UBool bCatenatePrealloc
;
51 typedef struct WLine WLine
;
55 }; //struct to store one line of wchar_t string
57 enum FnType
{ Fn_ICU
, Fn_STD
};
58 typedef FnType FnType
;
59 typedef void (*ICUStringPerfFn
)(const UChar
* src
,int32_t srcLen
, UnicodeString s0
);
60 typedef void (*StdStringPerfFn
)(const wchar_t* src
,int32_t srcLen
, stlstring s0
);
63 class StringPerfFunction
: public UPerfFunction
67 virtual long getEventsPerIteration(){
69 if (catICU
) { delete catICU
;}
70 if (catStd
) { delete catStd
;}
72 if (bCatenatePrealloc
) {
74 int to_alloc
= loops
* MAXNUMLINES
* (MAXSRCLEN
+ catenate_STRLEN
);
75 catICU
= new UnicodeString(to_alloc
,'a',0);
76 //catICU = new UnicodeString();
78 catStd
= new stlstring();
79 //catStd -> reserve(loops * MAXNUMLINES * (MAXSRCLEN + catenate_STRLEN));
80 catStd
-> reserve(110000000);
82 catICU
= new UnicodeString();
83 catStd
= new stlstring();
89 virtual void call(UErrorCode
* status
)
93 for(int32_t i
= 0; i
< numLines_
; i
++){
94 if (fnType_
==Fn_ICU
) {
95 (*fn1_
)(lines_
[i
].name
,lines_
[i
].len
,uS0_
[i
]);
97 (*fn2_
)(wlines_
[i
].name
,wlines_
[i
].len
,sS0_
[i
]);
101 for(int32_t i
= 0; i
< numLines_
; i
++){
102 if (fnType_
==Fn_ICU
) {
103 (*fn1_
)(lines_
[i
].name
,-1,uS0_
[i
]);
105 (*fn2_
)(wlines_
[i
].name
,-1,sS0_
[i
]);
111 if (fnType_
==Fn_ICU
) {
112 (*fn1_
)(src_
,srcLen_
,*ubulk_
);
114 (*fn2_
)(wsrc_
,wsrcLen_
,*sbulk_
);
117 if (fnType_
==Fn_ICU
) {
118 (*fn1_
)(src_
,-1,*ubulk_
);
120 (*fn2_
)(wsrc_
,-1,*sbulk_
);
126 virtual long getOperationsPerIteration()
128 if(line_mode_
==TRUE
){
135 StringPerfFunction(ICUStringPerfFn func
, ULine
* srcLines
, int32_t srcNumLines
, UBool uselen
)
141 numLines_
=srcNumLines
;
150 uS0_
=new UnicodeString
[numLines_
];
151 for(int32_t i
=0; i
<numLines_
; i
++) {
152 uS0_
[i
]=UnicodeString(lines_
[i
].name
, lines_
[i
].len
);
159 StringPerfFunction(StdStringPerfFn func
, ULine
* srcLines
, int32_t srcNumLines
, UBool uselen
)
165 numLines_
=srcNumLines
;
178 //fillin wlines_[], sS0_[]
179 prepareLinesForStd();
182 StringPerfFunction(ICUStringPerfFn func
, UChar
* source
, int32_t sourceLen
, UBool uselen
)
191 src_
= new UChar
[sourceLen
];
192 memcpy(src_
, source
, sourceLen
* U_SIZEOF_UCHAR
);
200 ubulk_
=new UnicodeString(src_
,srcLen_
);
204 StringPerfFunction(StdStringPerfFn func
, UChar
* source
, int32_t sourceLen
, UBool uselen
)
213 src_
= new UChar
[sourceLen
];
214 memcpy(src_
, source
, sourceLen
* U_SIZEOF_UCHAR
);
222 //fillin wsrc_, sbulk_
227 ~StringPerfFunction()
240 void prepareLinesForStd(void)
242 UErrorCode err
=U_ZERO_ERROR
;
244 wlines_
=new WLine
[numLines_
];
246 int32_t wcap
= UPRV_LENGTHOF(ws
);
250 sS0_
=new stlstring
[numLines_
];
251 for(int32_t i
=0; i
<numLines_
; i
++) {
253 wcs
= u_strToWCS(ws
, wcap
, &wl
, lines_
[i
].name
, lines_
[i
].len
, &err
);
254 memcpy(wlines_
[i
].name
, wcs
, wl
* sizeof(wchar_t));
256 sS0_
[i
]=stlstring(wlines_
[i
].name
, wlines_
[i
].len
);
258 wcs
= u_strToWCS(ws
, wcap
, &wl
, lines_
[i
].name
, lines_
[i
].len
-1, &err
);
259 memcpy(wlines_
[i
].name
, wcs
, wl
*sizeof(wchar_t));
261 sS0_
[i
]=stlstring(wlines_
[i
].name
, wlines_
[i
].len
+1);
264 if (U_FAILURE(err
)) {
271 void prepareBulkForStd(void)
273 UErrorCode err
=U_ZERO_ERROR
;
275 const UChar
* uSrc
= src_
;
276 int32_t uSrcLen
= srcLen_
;
277 wchar_t* wDest
= NULL
;
278 int32_t wDestLen
= 0;
283 u_strToWCS(wDest
,wDestLen
,&reqLen
,uSrc
,uSrcLen
,&err
);
285 if(err
== U_BUFFER_OVERFLOW_ERROR
){
287 wDest
=(wchar_t*) malloc(sizeof(wchar_t) * (reqLen
));
289 u_strToWCS(wDest
,wDestLen
,&reqLen
,uSrc
,uSrcLen
,&err
);
292 if (U_SUCCESS(err
)) {
295 sbulk_
=new stlstring(wsrc_
,wsrcLen_
);
300 u_strToWCS(wDest
,wDestLen
,&reqLen
,uSrc
,uSrcLen
-1,&err
);
302 if(err
== U_BUFFER_OVERFLOW_ERROR
){
304 wDest
=(wchar_t*) malloc(sizeof(wchar_t) * (reqLen
+1));
306 u_strToWCS(wDest
,wDestLen
,&reqLen
,uSrc
,uSrcLen
-1,&err
);
309 if (U_SUCCESS(err
)) {
312 sbulk_
=new stlstring(wsrc_
);
321 ICUStringPerfFn fn1_
;
322 StdStringPerfFn fn2_
;
335 //added for preparing testing data
338 UnicodeString
* ubulk_
;
344 class StringPerformanceTest
: public UPerfTest
347 StringPerformanceTest(int32_t argc
, const char *argv
[], UErrorCode
&status
);
348 ~StringPerformanceTest();
349 virtual UPerfFunction
* runIndexedTest(int32_t index
, UBool exec
,
352 UPerfFunction
* TestCtor();
353 UPerfFunction
* TestCtor1();
354 UPerfFunction
* TestCtor2();
355 UPerfFunction
* TestCtor3();
356 UPerfFunction
* TestAssign();
357 UPerfFunction
* TestAssign1();
358 UPerfFunction
* TestAssign2();
359 UPerfFunction
* TestGetch();
360 UPerfFunction
* TestCatenate();
361 UPerfFunction
* TestScan();
362 UPerfFunction
* TestScan1();
363 UPerfFunction
* TestScan2();
365 UPerfFunction
* TestStdLibCtor();
366 UPerfFunction
* TestStdLibCtor1();
367 UPerfFunction
* TestStdLibCtor2();
368 UPerfFunction
* TestStdLibCtor3();
369 UPerfFunction
* TestStdLibAssign();
370 UPerfFunction
* TestStdLibAssign1();
371 UPerfFunction
* TestStdLibAssign2();
372 UPerfFunction
* TestStdLibGetch();
373 UPerfFunction
* TestStdLibCatenate();
374 UPerfFunction
* TestStdLibScan();
375 UPerfFunction
* TestStdLibScan1();
376 UPerfFunction
* TestStdLibScan2();
382 int32_t StrBufferLen
;
387 inline void ctor(const UChar
* src
,int32_t srcLen
, UnicodeString s0
)
392 inline void ctor1(const UChar
* src
,int32_t srcLen
, UnicodeString s0
)
394 UnicodeString
b(uTESTCHAR1
);
397 inline void ctor2(const UChar
* src
,int32_t srcLen
, UnicodeString s0
)
399 UnicodeString
c(uEMPTY
);
402 inline void ctor3(const UChar
* src
,int32_t srcLen
, UnicodeString s0
)
404 UnicodeString
d(src
,srcLen
);
407 inline UnicodeString
icu_assign_helper(const UChar
* src
,int32_t srcLen
)
409 if (srcLen
==-1) { return src
;}
410 else { return UnicodeString(src
, srcLen
);}
413 inline void assign(const UChar
* src
,int32_t srcLen
, UnicodeString s0
)
415 unistr
= icu_assign_helper(src
,srcLen
);
418 inline void assign1(const UChar
* src
,int32_t srcLen
, UnicodeString s0
)
420 unistr
.setTo(src
, srcLen
);
423 inline void assign2(const UChar
* src
,int32_t srcLen
, UnicodeString s0
)
428 inline void getch(const UChar
* src
,int32_t srcLen
, UnicodeString s0
)
434 inline void catenate(const UChar
* src
,int32_t srcLen
, UnicodeString s0
)
436 UTimer mystart
, mystop
;
437 utimer_getTime(&mystart
);
441 utimer_getTime(&mystop
);
442 double mytime
= utimer_getDeltaSeconds(&mystart
,&mystop
);
443 printf("\nmytime=%f \n", mytime
);
445 *catICU
+= uCatenate_STR
;
448 volatile int scan_idx
;
449 U_STRING_DECL(SCAN1
, "123", 3);
451 inline void scan(const UChar
* src
,int32_t srcLen
, UnicodeString s0
)
454 scan_idx
= uScan_STRING
.indexOf(c
);
457 inline void scan1(const UChar
* src
,int32_t srcLen
, UnicodeString s0
)
459 scan_idx
= uScan_STRING
.indexOf(SCAN1
,3);
462 inline void scan2(const UChar
* src
,int32_t srcLen
, UnicodeString s0
)
466 scan_idx
= uScan_STRING
.indexOf(c1
);
467 scan_idx
= uScan_STRING
.indexOf(c2
);
471 inline void StdLibCtor(const wchar_t* src
,int32_t srcLen
, stlstring s0
)
476 inline void StdLibCtor1(const wchar_t* src
,int32_t srcLen
, stlstring s0
)
478 stlstring
b(simulate
);
481 inline void StdLibCtor2(const wchar_t* src
,int32_t srcLen
, stlstring s0
)
486 inline void StdLibCtor3(const wchar_t* src
,int32_t srcLen
, stlstring s0
)
491 stlstring
d(src
, srcLen
);
495 inline stlstring
stl_assign_helper(const wchar_t* src
,int32_t srcLen
)
497 if (srcLen
==-1) { return src
;}
498 else { return stlstring(src
, srcLen
);}
501 inline void StdLibAssign(const wchar_t* src
,int32_t srcLen
, stlstring s0
)
503 stlstr
= stl_assign_helper(src
,srcLen
);
506 inline void StdLibAssign1(const wchar_t* src
,int32_t srcLen
, stlstring s0
)
508 if (srcLen
==-1) { stlstr
=src
;}
509 else { stlstr
.assign(src
, srcLen
);}
512 inline void StdLibAssign2(const wchar_t* src
,int32_t srcLen
, stlstring s0
)
517 inline void StdLibGetch(const wchar_t* src
,int32_t srcLen
, stlstring s0
)
522 inline void StdLibCatenate(const wchar_t* src
,int32_t srcLen
, stlstring s0
)
524 UTimer mystart
, mystop
;
525 utimer_getTime(&mystart
);
528 *catStd
+= sCatenate_STR
;
530 utimer_getTime(&mystop
);
531 double mytime
= utimer_getDeltaSeconds(&mystart
,&mystop
);
532 printf("\nmytime=%f \n", mytime
);
536 inline void StdLibScan(const wchar_t* src
,int32_t srcLen
, stlstring s0
)
538 scan_idx
= (int) sScan_STRING
.find('.');
541 inline void StdLibScan1(const wchar_t* src
,int32_t srcLen
, stlstring s0
)
543 scan_idx
= (int) sScan_STRING
.find(L
"123");
546 inline void StdLibScan2(const wchar_t* src
,int32_t srcLen
, stlstring s0
)
548 scan_idx
= (int) sScan_STRING
.find_first_of(L
"sm");
551 #endif // STRINGPERF_H