2 ***********************************************************************
3 * © 2016 and later: Unicode, Inc. and others.
4 * License & terms of use: http://www.unicode.org/copyright.html#License
5 ***********************************************************************
6 ***********************************************************************
7 * Copyright (c) 2002-2016, International Business Machines
8 * Corporation and others. All Rights Reserved.
9 ***********************************************************************
15 #include "unicode/utypes.h"
16 #include "unicode/unistr.h"
18 #include "unicode/uperf.h"
24 typedef std::wstring stlstring
;
26 /* Define all constants for test case operations */
27 #define MAXNUMLINES 40000 //Max number of lines in a test data file
28 #define MAXSRCLEN 20 //Max length of one line. maybe a larger number, but it need more mem
29 #define LOOPS 100 //Iterations
31 #define catenate_STRLEN 2
33 const UChar uTESTCHAR1
= 'a';
34 const wchar_t wTESTCHAR1
= 'a';
35 const UnicodeString uEMPTY
;
36 const stlstring sEMPTY
;
39 // Simulate construction with a single-char string for basic_string
40 wchar_t simulate
[2]={wTESTCHAR1
, 0};
42 /* Constants for scan operation */
43 U_STRING_DECL(scan_STRING
, "Dot. 123. Some more data.", 25);
44 const UnicodeString uScan_STRING
=UnicodeString(scan_STRING
);
45 const stlstring sScan_STRING
=stlstring(L
"Dot. 123. Some more data.");
47 /* global variables or constants for concatenation operation */
48 U_STRING_DECL(uCatenate_STR
, "!!", 2);
49 const stlstring sCatenate_STR
=stlstring(L
"!!");
50 static UnicodeString
* catICU
;
51 static stlstring
* catStd
;
52 UBool bCatenatePrealloc
;
55 typedef struct WLine WLine
;
59 }; //struct to store one line of wchar_t string
61 enum FnType
{ Fn_ICU
, Fn_STD
};
62 typedef FnType FnType
;
63 typedef void (*ICUStringPerfFn
)(const UChar
* src
,int32_t srcLen
, UnicodeString s0
);
64 typedef void (*StdStringPerfFn
)(const wchar_t* src
,int32_t srcLen
, stlstring s0
);
67 class StringPerfFunction
: public UPerfFunction
71 virtual long getEventsPerIteration(){
73 if (catICU
) { delete catICU
;}
74 if (catStd
) { delete catStd
;}
76 if (bCatenatePrealloc
) {
78 int to_alloc
= loops
* MAXNUMLINES
* (MAXSRCLEN
+ catenate_STRLEN
);
79 catICU
= new UnicodeString(to_alloc
,'a',0);
80 //catICU = new UnicodeString();
82 catStd
= new stlstring();
83 //catStd -> reserve(loops * MAXNUMLINES * (MAXSRCLEN + catenate_STRLEN));
84 catStd
-> reserve(110000000);
86 catICU
= new UnicodeString();
87 catStd
= new stlstring();
93 virtual void call(UErrorCode
* status
)
97 for(int32_t i
= 0; i
< numLines_
; i
++){
98 if (fnType_
==Fn_ICU
) {
99 (*fn1_
)(lines_
[i
].name
,lines_
[i
].len
,uS0_
[i
]);
101 (*fn2_
)(wlines_
[i
].name
,wlines_
[i
].len
,sS0_
[i
]);
105 for(int32_t i
= 0; i
< numLines_
; i
++){
106 if (fnType_
==Fn_ICU
) {
107 (*fn1_
)(lines_
[i
].name
,-1,uS0_
[i
]);
109 (*fn2_
)(wlines_
[i
].name
,-1,sS0_
[i
]);
115 if (fnType_
==Fn_ICU
) {
116 (*fn1_
)(src_
,srcLen_
,*ubulk_
);
118 (*fn2_
)(wsrc_
,wsrcLen_
,*sbulk_
);
121 if (fnType_
==Fn_ICU
) {
122 (*fn1_
)(src_
,-1,*ubulk_
);
124 (*fn2_
)(wsrc_
,-1,*sbulk_
);
130 virtual long getOperationsPerIteration()
132 if(line_mode_
==TRUE
){
139 StringPerfFunction(ICUStringPerfFn func
, ULine
* srcLines
, int32_t srcNumLines
, UBool uselen
)
145 numLines_
=srcNumLines
;
154 uS0_
=new UnicodeString
[numLines_
];
155 for(int32_t i
=0; i
<numLines_
; i
++) {
156 uS0_
[i
]=UnicodeString(lines_
[i
].name
, lines_
[i
].len
);
163 StringPerfFunction(StdStringPerfFn func
, ULine
* srcLines
, int32_t srcNumLines
, UBool uselen
)
169 numLines_
=srcNumLines
;
182 //fillin wlines_[], sS0_[]
183 prepareLinesForStd();
186 StringPerfFunction(ICUStringPerfFn func
, UChar
* source
, int32_t sourceLen
, UBool uselen
)
195 src_
= new UChar
[sourceLen
];
196 memcpy(src_
, source
, sourceLen
* U_SIZEOF_UCHAR
);
204 ubulk_
=new UnicodeString(src_
,srcLen_
);
208 StringPerfFunction(StdStringPerfFn func
, UChar
* source
, int32_t sourceLen
, UBool uselen
)
217 src_
= new UChar
[sourceLen
];
218 memcpy(src_
, source
, sourceLen
* U_SIZEOF_UCHAR
);
226 //fillin wsrc_, sbulk_
231 ~StringPerfFunction()
244 void prepareLinesForStd(void)
246 UErrorCode err
=U_ZERO_ERROR
;
248 wlines_
=new WLine
[numLines_
];
250 int32_t wcap
= UPRV_LENGTHOF(ws
);
254 sS0_
=new stlstring
[numLines_
];
255 for(int32_t i
=0; i
<numLines_
; i
++) {
257 wcs
= u_strToWCS(ws
, wcap
, &wl
, lines_
[i
].name
, lines_
[i
].len
, &err
);
258 memcpy(wlines_
[i
].name
, wcs
, wl
* sizeof(wchar_t));
260 sS0_
[i
]=stlstring(wlines_
[i
].name
, wlines_
[i
].len
);
262 wcs
= u_strToWCS(ws
, wcap
, &wl
, lines_
[i
].name
, lines_
[i
].len
-1, &err
);
263 memcpy(wlines_
[i
].name
, wcs
, wl
*sizeof(wchar_t));
265 sS0_
[i
]=stlstring(wlines_
[i
].name
, wlines_
[i
].len
+1);
268 if (U_FAILURE(err
)) {
275 void prepareBulkForStd(void)
277 UErrorCode err
=U_ZERO_ERROR
;
279 const UChar
* uSrc
= src_
;
280 int32_t uSrcLen
= srcLen_
;
281 wchar_t* wDest
= NULL
;
282 int32_t wDestLen
= 0;
287 u_strToWCS(wDest
,wDestLen
,&reqLen
,uSrc
,uSrcLen
,&err
);
289 if(err
== U_BUFFER_OVERFLOW_ERROR
){
291 wDest
=(wchar_t*) malloc(sizeof(wchar_t) * (reqLen
));
293 u_strToWCS(wDest
,wDestLen
,&reqLen
,uSrc
,uSrcLen
,&err
);
296 if (U_SUCCESS(err
)) {
299 sbulk_
=new stlstring(wsrc_
,wsrcLen_
);
304 u_strToWCS(wDest
,wDestLen
,&reqLen
,uSrc
,uSrcLen
-1,&err
);
306 if(err
== U_BUFFER_OVERFLOW_ERROR
){
308 wDest
=(wchar_t*) malloc(sizeof(wchar_t) * (reqLen
+1));
310 u_strToWCS(wDest
,wDestLen
,&reqLen
,uSrc
,uSrcLen
-1,&err
);
313 if (U_SUCCESS(err
)) {
316 sbulk_
=new stlstring(wsrc_
);
325 ICUStringPerfFn fn1_
;
326 StdStringPerfFn fn2_
;
339 //added for preparing testing data
342 UnicodeString
* ubulk_
;
348 class StringPerformanceTest
: public UPerfTest
351 StringPerformanceTest(int32_t argc
, const char *argv
[], UErrorCode
&status
);
352 ~StringPerformanceTest();
353 virtual UPerfFunction
* runIndexedTest(int32_t index
, UBool exec
,
356 UPerfFunction
* TestCtor();
357 UPerfFunction
* TestCtor1();
358 UPerfFunction
* TestCtor2();
359 UPerfFunction
* TestCtor3();
360 UPerfFunction
* TestAssign();
361 UPerfFunction
* TestAssign1();
362 UPerfFunction
* TestAssign2();
363 UPerfFunction
* TestGetch();
364 UPerfFunction
* TestCatenate();
365 UPerfFunction
* TestScan();
366 UPerfFunction
* TestScan1();
367 UPerfFunction
* TestScan2();
369 UPerfFunction
* TestStdLibCtor();
370 UPerfFunction
* TestStdLibCtor1();
371 UPerfFunction
* TestStdLibCtor2();
372 UPerfFunction
* TestStdLibCtor3();
373 UPerfFunction
* TestStdLibAssign();
374 UPerfFunction
* TestStdLibAssign1();
375 UPerfFunction
* TestStdLibAssign2();
376 UPerfFunction
* TestStdLibGetch();
377 UPerfFunction
* TestStdLibCatenate();
378 UPerfFunction
* TestStdLibScan();
379 UPerfFunction
* TestStdLibScan1();
380 UPerfFunction
* TestStdLibScan2();
386 int32_t StrBufferLen
;
391 inline void ctor(const UChar
* src
,int32_t srcLen
, UnicodeString s0
)
396 inline void ctor1(const UChar
* src
,int32_t srcLen
, UnicodeString s0
)
398 UnicodeString
b(uTESTCHAR1
);
401 inline void ctor2(const UChar
* src
,int32_t srcLen
, UnicodeString s0
)
403 UnicodeString
c(uEMPTY
);
406 inline void ctor3(const UChar
* src
,int32_t srcLen
, UnicodeString s0
)
408 UnicodeString
d(src
,srcLen
);
411 inline UnicodeString
icu_assign_helper(const UChar
* src
,int32_t srcLen
)
413 if (srcLen
==-1) { return src
;}
414 else { return UnicodeString(src
, srcLen
);}
417 inline void assign(const UChar
* src
,int32_t srcLen
, UnicodeString s0
)
419 unistr
= icu_assign_helper(src
,srcLen
);
422 inline void assign1(const UChar
* src
,int32_t srcLen
, UnicodeString s0
)
424 unistr
.setTo(src
, srcLen
);
427 inline void assign2(const UChar
* src
,int32_t srcLen
, UnicodeString s0
)
432 inline void getch(const UChar
* src
,int32_t srcLen
, UnicodeString s0
)
438 inline void catenate(const UChar
* src
,int32_t srcLen
, UnicodeString s0
)
440 UTimer mystart
, mystop
;
441 utimer_getTime(&mystart
);
445 utimer_getTime(&mystop
);
446 double mytime
= utimer_getDeltaSeconds(&mystart
,&mystop
);
447 printf("\nmytime=%f \n", mytime
);
449 *catICU
+= uCatenate_STR
;
452 volatile int scan_idx
;
453 U_STRING_DECL(SCAN1
, "123", 3);
455 inline void scan(const UChar
* src
,int32_t srcLen
, UnicodeString s0
)
458 scan_idx
= uScan_STRING
.indexOf(c
);
461 inline void scan1(const UChar
* src
,int32_t srcLen
, UnicodeString s0
)
463 scan_idx
= uScan_STRING
.indexOf(SCAN1
,3);
466 inline void scan2(const UChar
* src
,int32_t srcLen
, UnicodeString s0
)
470 scan_idx
= uScan_STRING
.indexOf(c1
);
471 scan_idx
= uScan_STRING
.indexOf(c2
);
475 inline void StdLibCtor(const wchar_t* src
,int32_t srcLen
, stlstring s0
)
480 inline void StdLibCtor1(const wchar_t* src
,int32_t srcLen
, stlstring s0
)
482 stlstring
b(simulate
);
485 inline void StdLibCtor2(const wchar_t* src
,int32_t srcLen
, stlstring s0
)
490 inline void StdLibCtor3(const wchar_t* src
,int32_t srcLen
, stlstring s0
)
495 stlstring
d(src
, srcLen
);
499 inline stlstring
stl_assign_helper(const wchar_t* src
,int32_t srcLen
)
501 if (srcLen
==-1) { return src
;}
502 else { return stlstring(src
, srcLen
);}
505 inline void StdLibAssign(const wchar_t* src
,int32_t srcLen
, stlstring s0
)
507 stlstr
= stl_assign_helper(src
,srcLen
);
510 inline void StdLibAssign1(const wchar_t* src
,int32_t srcLen
, stlstring s0
)
512 if (srcLen
==-1) { stlstr
=src
;}
513 else { stlstr
.assign(src
, srcLen
);}
516 inline void StdLibAssign2(const wchar_t* src
,int32_t srcLen
, stlstring s0
)
521 inline void StdLibGetch(const wchar_t* src
,int32_t srcLen
, stlstring s0
)
526 inline void StdLibCatenate(const wchar_t* src
,int32_t srcLen
, stlstring s0
)
528 UTimer mystart
, mystop
;
529 utimer_getTime(&mystart
);
532 *catStd
+= sCatenate_STR
;
534 utimer_getTime(&mystop
);
535 double mytime
= utimer_getDeltaSeconds(&mystart
,&mystop
);
536 printf("\nmytime=%f \n", mytime
);
540 inline void StdLibScan(const wchar_t* src
,int32_t srcLen
, stlstring s0
)
542 scan_idx
= (int) sScan_STRING
.find('.');
545 inline void StdLibScan1(const wchar_t* src
,int32_t srcLen
, stlstring s0
)
547 scan_idx
= (int) sScan_STRING
.find(L
"123");
550 inline void StdLibScan2(const wchar_t* src
,int32_t srcLen
, stlstring s0
)
552 scan_idx
= (int) sScan_STRING
.find_first_of(L
"sm");
555 #endif // STRINGPERF_H