]> git.saurik.com Git - apple/icu.git/blob - icuSources/test/perf/ustrperf/stringperf.h
ICU-57149.0.1.tar.gz
[apple/icu.git] / icuSources / test / perf / ustrperf / stringperf.h
1 /*
2 **********************************************************************
3 * Copyright (c) 2002-2016, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 **********************************************************************
6 */
7 #ifndef _STRINGPERF_H
8 #define _STRINGPERF_H
9
10 #include "cmemory.h"
11 #include "unicode/utypes.h"
12 #include "unicode/unistr.h"
13
14 #include "unicode/uperf.h"
15
16 #include <string.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19
20 typedef std::wstring stlstring;
21
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
26 //#define LOOPS 10
27 #define catenate_STRLEN 2
28
29 const UChar uTESTCHAR1 = 'a';
30 const wchar_t wTESTCHAR1 = 'a';
31 const UnicodeString uEMPTY;
32 const stlstring sEMPTY;
33 UnicodeString unistr;
34 stlstring stlstr;
35 // Simulate construction with a single-char string for basic_string
36 wchar_t simulate[2]={wTESTCHAR1, 0};
37
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.");
42
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;
49
50 /* type defines */
51 typedef struct WLine WLine;
52 struct WLine {
53 wchar_t name[100];
54 int32_t len;
55 }; //struct to store one line of wchar_t string
56
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);
61
62
63 class StringPerfFunction : public UPerfFunction
64 {
65 public:
66
67 virtual long getEventsPerIteration(){
68 int loops = LOOPS;
69 if (catICU) { delete catICU;}
70 if (catStd) { delete catStd;}
71
72 if (bCatenatePrealloc) {
73
74 int to_alloc = loops * MAXNUMLINES * (MAXSRCLEN + catenate_STRLEN);
75 catICU = new UnicodeString(to_alloc,'a',0);
76 //catICU = new UnicodeString();
77
78 catStd = new stlstring();
79 //catStd -> reserve(loops * MAXNUMLINES * (MAXSRCLEN + catenate_STRLEN));
80 catStd -> reserve(110000000);
81 } else {
82 catICU = new UnicodeString();
83 catStd = new stlstring();
84 }
85
86 return -1;
87 }
88
89 virtual void call(UErrorCode* status)
90 {
91 if(line_mode_==TRUE){
92 if(uselen_){
93 for(int32_t i = 0; i< numLines_; i++){
94 if (fnType_==Fn_ICU) {
95 (*fn1_)(lines_[i].name,lines_[i].len,uS0_[i]);
96 } else {
97 (*fn2_)(wlines_[i].name,wlines_[i].len,sS0_[i]);
98 }
99 }
100 }else{
101 for(int32_t i = 0; i< numLines_; i++){
102 if (fnType_==Fn_ICU) {
103 (*fn1_)(lines_[i].name,-1,uS0_[i]);
104 } else {
105 (*fn2_)(wlines_[i].name,-1,sS0_[i]);
106 }
107 }
108 }
109 }else{
110 if(uselen_){
111 if (fnType_==Fn_ICU) {
112 (*fn1_)(src_,srcLen_,*ubulk_);
113 } else {
114 (*fn2_)(wsrc_,wsrcLen_,*sbulk_);
115 }
116 }else{
117 if (fnType_==Fn_ICU) {
118 (*fn1_)(src_,-1,*ubulk_);
119 } else {
120 (*fn2_)(wsrc_,-1,*sbulk_);
121 }
122 }
123 }
124 }
125
126 virtual long getOperationsPerIteration()
127 {
128 if(line_mode_==TRUE){
129 return numLines_;
130 }else{
131 return 1;
132 }
133 }
134
135 StringPerfFunction(ICUStringPerfFn func, ULine* srcLines, int32_t srcNumLines, UBool uselen)
136 {
137
138 fn1_ = func;
139 lines_=srcLines;
140 wlines_=NULL;
141 numLines_=srcNumLines;
142 uselen_=uselen;
143 line_mode_=TRUE;
144 src_ = NULL;
145 srcLen_ = 0;
146 wsrc_ = NULL;
147 wsrcLen_ = 0;
148 fnType_ = Fn_ICU;
149
150 uS0_=new UnicodeString[numLines_];
151 for(int32_t i=0; i<numLines_; i++) {
152 uS0_[i]=UnicodeString(lines_[i].name, lines_[i].len);
153 }
154 sS0_=NULL;
155 ubulk_=NULL;
156 sbulk_=NULL;
157 }
158
159 StringPerfFunction(StdStringPerfFn func, ULine* srcLines, int32_t srcNumLines, UBool uselen)
160 {
161
162 fn2_ = func;
163 lines_=srcLines;
164 wlines_=NULL;
165 numLines_=srcNumLines;
166 uselen_=uselen;
167 line_mode_=TRUE;
168 src_ = NULL;
169 srcLen_ = 0;
170 wsrc_ = NULL;
171 wsrcLen_ = 0;
172 fnType_ = Fn_STD;
173
174 uS0_=NULL;
175 ubulk_=NULL;
176 sbulk_=NULL;
177
178 //fillin wlines_[], sS0_[]
179 prepareLinesForStd();
180 }
181
182 StringPerfFunction(ICUStringPerfFn func, UChar* source, int32_t sourceLen, UBool uselen)
183 {
184
185 fn1_ = func;
186 lines_=NULL;
187 wlines_=NULL;
188 numLines_=0;
189 uselen_=uselen;
190 line_mode_=FALSE;
191 src_ = new UChar[sourceLen];
192 memcpy(src_, source, sourceLen * U_SIZEOF_UCHAR);
193 srcLen_ = sourceLen;
194 wsrc_ = NULL;
195 wsrcLen_ = 0;
196 fnType_ = Fn_ICU;
197
198 uS0_=NULL;
199 sS0_=NULL;
200 ubulk_=new UnicodeString(src_,srcLen_);
201 sbulk_=NULL;
202 }
203
204 StringPerfFunction(StdStringPerfFn func, UChar* source, int32_t sourceLen, UBool uselen)
205 {
206
207 fn2_ = func;
208 lines_=NULL;
209 wlines_=NULL;
210 numLines_=0;
211 uselen_=uselen;
212 line_mode_=FALSE;
213 src_ = new UChar[sourceLen];
214 memcpy(src_, source, sourceLen * U_SIZEOF_UCHAR);
215 srcLen_ = sourceLen;
216 fnType_ = Fn_STD;
217
218 uS0_=NULL;
219 sS0_=NULL;
220 ubulk_=NULL;
221
222 //fillin wsrc_, sbulk_
223 prepareBulkForStd();
224
225 }
226
227 ~StringPerfFunction()
228 {
229 //free(src_);
230 free(wsrc_);
231 delete[] src_;
232 delete ubulk_;
233 delete sbulk_;
234 delete[] uS0_;
235 delete[] sS0_;
236 delete[] wlines_;
237 }
238
239 private:
240 void prepareLinesForStd(void)
241 {
242 UErrorCode err=U_ZERO_ERROR;
243
244 wlines_=new WLine[numLines_];
245 wchar_t ws[100];
246 int32_t wcap = UPRV_LENGTHOF(ws);
247 int32_t wl;
248 wchar_t* wcs;
249
250 sS0_=new stlstring[numLines_];
251 for(int32_t i=0; i<numLines_; i++) {
252 if(uselen_) {
253 wcs = u_strToWCS(ws, wcap, &wl, lines_[i].name, lines_[i].len, &err);
254 memcpy(wlines_[i].name, wcs, wl * sizeof(wchar_t));
255 wlines_[i].len = wl;
256 sS0_[i]=stlstring(wlines_[i].name, wlines_[i].len);
257 } else {
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));
260 wlines_[i].len = wl;
261 sS0_[i]=stlstring(wlines_[i].name, wlines_[i].len+1);
262 }
263
264 if (U_FAILURE(err)) {
265 return;
266 }
267 }
268
269 }
270
271 void prepareBulkForStd(void)
272 {
273 UErrorCode err=U_ZERO_ERROR;
274
275 const UChar* uSrc = src_;
276 int32_t uSrcLen = srcLen_;
277 wchar_t* wDest = NULL;
278 int32_t wDestLen = 0;
279 int32_t reqLen= 0 ;
280
281 if(uselen_) {
282 /* pre-flight*/
283 u_strToWCS(wDest,wDestLen,&reqLen,uSrc,uSrcLen,&err);
284
285 if(err == U_BUFFER_OVERFLOW_ERROR){
286 err=U_ZERO_ERROR;
287 wDest =(wchar_t*) malloc(sizeof(wchar_t) * (reqLen));
288 wDestLen = reqLen;
289 u_strToWCS(wDest,wDestLen,&reqLen,uSrc,uSrcLen,&err);
290 }
291
292 if (U_SUCCESS(err)) {
293 wsrc_ = wDest;
294 wsrcLen_ = wDestLen;
295 sbulk_=new stlstring(wsrc_,wsrcLen_);
296 }
297
298 } else {
299 /* pre-flight*/
300 u_strToWCS(wDest,wDestLen,&reqLen,uSrc,uSrcLen-1,&err);
301
302 if(err == U_BUFFER_OVERFLOW_ERROR){
303 err=U_ZERO_ERROR;
304 wDest =(wchar_t*) malloc(sizeof(wchar_t) * (reqLen+1));
305 wDestLen = reqLen+1;
306 u_strToWCS(wDest,wDestLen,&reqLen,uSrc,uSrcLen-1,&err);
307 }
308
309 if (U_SUCCESS(err)) {
310 wsrc_ = wDest;
311 wsrcLen_ = wDestLen;
312 sbulk_=new stlstring(wsrc_);
313 }
314 }
315
316 //free(wDest);
317 }
318
319
320 private:
321 ICUStringPerfFn fn1_;
322 StdStringPerfFn fn2_;
323
324 ULine* lines_;
325 WLine* wlines_;
326 int32_t numLines_;
327
328 UBool uselen_;
329 UChar* src_;
330 int32_t srcLen_;
331 wchar_t* wsrc_;
332 int32_t wsrcLen_;
333 UBool line_mode_;
334
335 //added for preparing testing data
336 UnicodeString* uS0_;
337 stlstring* sS0_;
338 UnicodeString* ubulk_;
339 stlstring* sbulk_;
340 FnType fnType_;
341 };
342
343
344 class StringPerformanceTest : public UPerfTest
345 {
346 public:
347 StringPerformanceTest(int32_t argc, const char *argv[], UErrorCode &status);
348 ~StringPerformanceTest();
349 virtual UPerfFunction* runIndexedTest(int32_t index, UBool exec,
350 const char *&name,
351 char *par = NULL);
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();
364
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();
377
378 private:
379 long COUNT_;
380 ULine* filelines_;
381 UChar* StrBuffer;
382 int32_t StrBufferLen;
383
384 };
385
386
387 inline void ctor(const UChar* src,int32_t srcLen, UnicodeString s0)
388 {
389 UnicodeString a;
390 }
391
392 inline void ctor1(const UChar* src,int32_t srcLen, UnicodeString s0)
393 {
394 UnicodeString b(uTESTCHAR1);
395 }
396
397 inline void ctor2(const UChar* src,int32_t srcLen, UnicodeString s0)
398 {
399 UnicodeString c(uEMPTY);
400 }
401
402 inline void ctor3(const UChar* src,int32_t srcLen, UnicodeString s0)
403 {
404 UnicodeString d(src,srcLen);
405 }
406
407 inline UnicodeString icu_assign_helper(const UChar* src,int32_t srcLen)
408 {
409 if (srcLen==-1) { return src;}
410 else { return UnicodeString(src, srcLen);}
411 }
412
413 inline void assign(const UChar* src,int32_t srcLen, UnicodeString s0)
414 {
415 unistr = icu_assign_helper(src,srcLen);
416 }
417
418 inline void assign1(const UChar* src,int32_t srcLen, UnicodeString s0)
419 {
420 unistr.setTo(src, srcLen);
421 }
422
423 inline void assign2(const UChar* src,int32_t srcLen, UnicodeString s0)
424 {
425 unistr = s0;
426 }
427
428 inline void getch(const UChar* src,int32_t srcLen, UnicodeString s0)
429 {
430 s0.charAt(0);
431 }
432
433
434 inline void catenate(const UChar* src,int32_t srcLen, UnicodeString s0)
435 {
436 UTimer mystart, mystop;
437 utimer_getTime(&mystart);
438
439 *catICU += s0;
440
441 utimer_getTime(&mystop);
442 double mytime = utimer_getDeltaSeconds(&mystart,&mystop);
443 printf("\nmytime=%f \n", mytime);
444
445 *catICU += uCatenate_STR;
446 }
447
448 volatile int scan_idx;
449 U_STRING_DECL(SCAN1, "123", 3);
450
451 inline void scan(const UChar* src,int32_t srcLen, UnicodeString s0)
452 {
453 UChar c='.';
454 scan_idx = uScan_STRING.indexOf(c);
455 }
456
457 inline void scan1(const UChar* src,int32_t srcLen, UnicodeString s0)
458 {
459 scan_idx = uScan_STRING.indexOf(SCAN1,3);
460 }
461
462 inline void scan2(const UChar* src,int32_t srcLen, UnicodeString s0)
463 {
464 UChar c1='s';
465 UChar c2='m';
466 scan_idx = uScan_STRING.indexOf(c1);
467 scan_idx = uScan_STRING.indexOf(c2);
468 }
469
470
471 inline void StdLibCtor(const wchar_t* src,int32_t srcLen, stlstring s0)
472 {
473 stlstring a;
474 }
475
476 inline void StdLibCtor1(const wchar_t* src,int32_t srcLen, stlstring s0)
477 {
478 stlstring b(simulate);
479 }
480
481 inline void StdLibCtor2(const wchar_t* src,int32_t srcLen, stlstring s0)
482 {
483 stlstring c(sEMPTY);
484 }
485
486 inline void StdLibCtor3(const wchar_t* src,int32_t srcLen, stlstring s0)
487 {
488 if (srcLen==-1) {
489 stlstring d(src);
490 }else {
491 stlstring d(src, srcLen);
492 }
493 }
494
495 inline stlstring stl_assign_helper(const wchar_t* src,int32_t srcLen)
496 {
497 if (srcLen==-1) { return src;}
498 else { return stlstring(src, srcLen);}
499 }
500
501 inline void StdLibAssign(const wchar_t* src,int32_t srcLen, stlstring s0)
502 {
503 stlstr = stl_assign_helper(src,srcLen);
504 }
505
506 inline void StdLibAssign1(const wchar_t* src,int32_t srcLen, stlstring s0)
507 {
508 if (srcLen==-1) { stlstr=src;}
509 else { stlstr.assign(src, srcLen);}
510 }
511
512 inline void StdLibAssign2(const wchar_t* src,int32_t srcLen, stlstring s0)
513 {
514 stlstr=s0;
515 }
516
517 inline void StdLibGetch(const wchar_t* src,int32_t srcLen, stlstring s0)
518 {
519 s0.at(0);
520 }
521
522 inline void StdLibCatenate(const wchar_t* src,int32_t srcLen, stlstring s0)
523 {
524 UTimer mystart, mystop;
525 utimer_getTime(&mystart);
526
527 *catStd += s0;
528 *catStd += sCatenate_STR;
529
530 utimer_getTime(&mystop);
531 double mytime = utimer_getDeltaSeconds(&mystart,&mystop);
532 printf("\nmytime=%f \n", mytime);
533
534 }
535
536 inline void StdLibScan(const wchar_t* src,int32_t srcLen, stlstring s0)
537 {
538 scan_idx = (int) sScan_STRING.find('.');
539 }
540
541 inline void StdLibScan1(const wchar_t* src,int32_t srcLen, stlstring s0)
542 {
543 scan_idx = (int) sScan_STRING.find(L"123");
544 }
545
546 inline void StdLibScan2(const wchar_t* src,int32_t srcLen, stlstring s0)
547 {
548 scan_idx = (int) sScan_STRING.find_first_of(L"sm");
549 }
550
551 #endif // STRINGPERF_H
552