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