]> git.saurik.com Git - apple/icu.git/blame - icuSources/test/perf/ustrperf/stringperf.cpp
ICU-8.11.tar.gz
[apple/icu.git] / icuSources / test / perf / ustrperf / stringperf.cpp
CommitLineData
374ca955
A
1/********************************************************************
2 * COPYRIGHT:
73c04bcf 3 * Copyright (C) 2002-2006 International Business Machines Corporation
374ca955
A
4 * and others. All Rights Reserved.
5 *
6 ********************************************************************/
7/*****************************************************************************
8* File stringperf.cpp
9*
10* Modification History:
11* Name Description
73c04bcf 12* Doug Wang Second version
374ca955
A
13* Doug Wang First Version
14******************************************************************************
15*/
16
17/**
18 * This program tests UnicodeString performance.
19 * APIs tested: UnicodeString
20 * ICU4C
21 * Windows 2000/XP, Linux
22 */
23
24#include "stringperf.h"
25
26
27int main(int argc, const char *argv[])
28{
29 UErrorCode status = U_ZERO_ERROR;
30
73c04bcf 31 bCatenatePrealloc=TRUE;
374ca955
A
32
33 StringPerformanceTest test(argc, argv, status);
73c04bcf 34 if (U_FAILURE(status)){
374ca955
A
35 return status;
36 }
73c04bcf
A
37
38 int loops = LOOPS;
39 if (bCatenatePrealloc) {
40 int to_alloc = loops * MAXNUMLINES * (MAXSRCLEN + catenate_STRLEN);
41 catICU = new UnicodeString(to_alloc,'a',0);
42 //catICU = new UnicodeString();
43
44 catStd = new stlstring();
45 catStd -> reserve(loops * MAXNUMLINES * (MAXSRCLEN + catenate_STRLEN));
46 //catStd -> reserve(110000000);
47 } else {
48 catICU = new UnicodeString();
49 catStd = new stlstring();
50 }
374ca955
A
51
52 if (test.run() == FALSE){
53 fprintf(stderr, "FAILED: Tests could not be run please check the "
73c04bcf 54 "arguments.\n");
374ca955
A
55 return -1;
56 }
57
73c04bcf
A
58 delete catICU;
59 delete catStd;
374ca955
A
60 return 0;
61}
62
63StringPerformanceTest::StringPerformanceTest(int32_t argc, const char *argv[],
73c04bcf
A
64 UErrorCode &status)
65 : UPerfTest(argc, argv, status)
374ca955 66{
73c04bcf
A
67 filelines_=NULL;
68 StrBuffer=NULL;
69 StrBufferLen=0;
374ca955 70
73c04bcf 71 int32_t len =0;
374ca955 72
73c04bcf
A
73 if (status== U_ILLEGAL_ARGUMENT_ERROR){
74 //fprintf(stderr,gUsageString, "stringperf");
75 return;
374ca955
A
76 }
77 if (U_FAILURE(status)){
78 fprintf(stderr, "FAILED to create UPerfTest object. Error: %s\n",
73c04bcf 79 u_errorName(status));
374ca955
A
80 return;
81 }
82
83
84 if(line_mode){
85 ULine* filelines = getLines(status);
86 if(U_FAILURE(status)){
87 fprintf(stderr, "FAILED to read lines from file and create UPerfTest object. Error: %s\n", u_errorName(status));
88 return;
89 }
73c04bcf
A
90
91 filelines_ = new ULine[numLines];
92 for (int i =0; i < numLines; i++) {
93 len = filelines[i].len;
94 filelines_[i].name = new UChar[len];
374ca955 95 filelines_[i].len = len;
73c04bcf
A
96 memcpy(filelines_[i].name, filelines[i].name, len * U_SIZEOF_UCHAR);
97 }
98
374ca955
A
99 }else if(bulk_mode){
100 int32_t srcLen = 0;
101 const UChar* src = getBuffer(srcLen,status);
102 if(U_FAILURE(status)){
103 fprintf(stderr, "FAILED to read buffer from file and create UPerfTest object. Error: %s\n", u_errorName(status));
104 return;
105 }
73c04bcf
A
106
107 StrBuffer = new UChar[srcLen];
108 StrBufferLen = srcLen;
109 memcpy(StrBuffer, src, srcLen * U_SIZEOF_UCHAR);
374ca955
A
110
111 }
112}
113
114StringPerformanceTest::~StringPerformanceTest()
115{
73c04bcf
A
116 delete[] filelines_;
117 delete[] StrBuffer;
374ca955
A
118}
119
120UPerfFunction* StringPerformanceTest::runIndexedTest(int32_t index, UBool exec,
73c04bcf
A
121 const char *&name,
122 char* par)
374ca955
A
123{
124 switch (index) {
125 TESTCASE(0, TestCtor);
73c04bcf
A
126 TESTCASE(1, TestCtor1);
127 TESTCASE(2, TestCtor2);
128 TESTCASE(3, TestCtor3);
129 TESTCASE(4, TestAssign);
130 TESTCASE(5, TestAssign1);
131 TESTCASE(6, TestAssign2);
132 TESTCASE(7, TestGetch);
133 TESTCASE(8, TestCatenate);
134 TESTCASE(9, TestScan);
135 TESTCASE(10, TestScan1);
136 TESTCASE(11, TestScan2);
137
138 TESTCASE(12, TestStdLibCtor);
139 TESTCASE(13, TestStdLibCtor1);
140 TESTCASE(14, TestStdLibCtor2);
141 TESTCASE(15, TestStdLibCtor3);
142 TESTCASE(16, TestStdLibAssign);
143 TESTCASE(17, TestStdLibAssign1);
144 TESTCASE(18, TestStdLibAssign2);
145 TESTCASE(19, TestStdLibGetch);
146 TESTCASE(20, TestStdLibCatenate);
147 TESTCASE(21, TestStdLibScan);
148 TESTCASE(22, TestStdLibScan1);
149 TESTCASE(23, TestStdLibScan2);
374ca955
A
150
151 default:
152 name = "";
153 return NULL;
154 }
155 return NULL;
156}
157
158UPerfFunction* StringPerformanceTest::TestCtor()
159{
73c04bcf
A
160 if (line_mode) {
161 return new StringPerfFunction(ctor, filelines_, numLines, uselen);
162 } else {
163 return new StringPerfFunction(ctor, StrBuffer, StrBufferLen, uselen);
164 }
374ca955
A
165}
166
167UPerfFunction* StringPerformanceTest::TestCtor1()
168{
73c04bcf
A
169 if (line_mode) {
170 return new StringPerfFunction(ctor1, filelines_, numLines, uselen);
171 } else {
172 return new StringPerfFunction(ctor1, StrBuffer, StrBufferLen, uselen);
173 }
374ca955
A
174}
175
176UPerfFunction* StringPerformanceTest::TestCtor2()
177{
73c04bcf
A
178 if (line_mode) {
179 return new StringPerfFunction(ctor2, filelines_, numLines, uselen);
180 } else {
181 return new StringPerfFunction(ctor2, StrBuffer, StrBufferLen, uselen);
182 }
374ca955
A
183}
184
185UPerfFunction* StringPerformanceTest::TestCtor3()
186{
73c04bcf
A
187 if (line_mode) {
188 return new StringPerfFunction(ctor3, filelines_, numLines, uselen);
189 } else {
190 return new StringPerfFunction(ctor3, StrBuffer, StrBufferLen, uselen);
191 }
374ca955
A
192}
193
194UPerfFunction* StringPerformanceTest::TestAssign()
195{
73c04bcf
A
196 if (line_mode) {
197 return new StringPerfFunction(assign, filelines_, numLines, uselen);
198 } else {
199 return new StringPerfFunction(assign, StrBuffer, StrBufferLen, uselen);
200 }
374ca955
A
201}
202
203UPerfFunction* StringPerformanceTest::TestAssign1()
204{
73c04bcf
A
205 if (line_mode) {
206 return new StringPerfFunction(assign1, filelines_, numLines, uselen);
207 } else {
208 return new StringPerfFunction(assign1, StrBuffer, StrBufferLen, uselen);
209 }
374ca955
A
210}
211
212UPerfFunction* StringPerformanceTest::TestAssign2()
213{
73c04bcf
A
214 if (line_mode) {
215 return new StringPerfFunction(assign2, filelines_, numLines, uselen);
216 } else {
217 return new StringPerfFunction(assign2, StrBuffer, StrBufferLen, uselen);
218 }
374ca955
A
219}
220
221
222UPerfFunction* StringPerformanceTest::TestGetch()
223{
73c04bcf
A
224 if (line_mode) {
225 return new StringPerfFunction(getch, filelines_, numLines, uselen);
226 } else {
227 return new StringPerfFunction(getch, StrBuffer, StrBufferLen, uselen);
228 }
374ca955
A
229}
230
231UPerfFunction* StringPerformanceTest::TestCatenate()
232{
73c04bcf
A
233 if (line_mode) {
234 return new StringPerfFunction(catenate, filelines_, numLines, uselen);
235 } else {
236 //return new StringPerfFunction(catenate, buffer, bufferLen, uselen);
237 return new StringPerfFunction(catenate, StrBuffer, StrBufferLen, uselen);
238 }
374ca955
A
239}
240
241UPerfFunction* StringPerformanceTest::TestScan()
242{
73c04bcf
A
243 if (line_mode) {
244 return new StringPerfFunction(scan, filelines_, numLines, uselen);
245 } else {
246 return new StringPerfFunction(scan, StrBuffer, StrBufferLen, uselen);
247 }
374ca955
A
248}
249
250UPerfFunction* StringPerformanceTest::TestScan1()
251{
73c04bcf
A
252 if (line_mode) {
253 return new StringPerfFunction(scan1, filelines_, numLines, uselen);
254 } else {
255 return new StringPerfFunction(scan1, StrBuffer, StrBufferLen, uselen);
256 }
374ca955
A
257}
258
259UPerfFunction* StringPerformanceTest::TestScan2()
260{
73c04bcf
A
261 if (line_mode) {
262 return new StringPerfFunction(scan2, filelines_, numLines, uselen);
263 } else {
264 return new StringPerfFunction(scan2, StrBuffer, StrBufferLen, uselen);
265 }
374ca955
A
266}
267
268UPerfFunction* StringPerformanceTest::TestStdLibCtor()
269{
73c04bcf
A
270 if (line_mode) {
271 return new StringPerfFunction(StdLibCtor, filelines_, numLines, uselen);
272 } else {
273 return new StringPerfFunction(StdLibCtor, StrBuffer, StrBufferLen, uselen);
274 }
374ca955
A
275}
276
277UPerfFunction* StringPerformanceTest::TestStdLibCtor1()
278{
73c04bcf
A
279 if (line_mode) {
280 return new StringPerfFunction(StdLibCtor1, filelines_, numLines, uselen);
281 } else {
282 return new StringPerfFunction(StdLibCtor1, StrBuffer, StrBufferLen, uselen);
283 }
374ca955
A
284}
285
286UPerfFunction* StringPerformanceTest::TestStdLibCtor2()
287{
73c04bcf
A
288 if (line_mode) {
289 return new StringPerfFunction(StdLibCtor2, filelines_, numLines, uselen);
290 } else {
291 return new StringPerfFunction(StdLibCtor2, StrBuffer, StrBufferLen, uselen);
292 }
374ca955
A
293}
294
295UPerfFunction* StringPerformanceTest::TestStdLibCtor3()
296{
73c04bcf
A
297 if (line_mode) {
298 return new StringPerfFunction(StdLibCtor3, filelines_, numLines, uselen);
299 } else {
300 return new StringPerfFunction(StdLibCtor3, StrBuffer, StrBufferLen, uselen);
301 }
374ca955
A
302}
303
304UPerfFunction* StringPerformanceTest::TestStdLibAssign()
305{
73c04bcf
A
306 if (line_mode) {
307 return new StringPerfFunction(StdLibAssign, filelines_, numLines, uselen);
308 } else {
309 return new StringPerfFunction(StdLibAssign, StrBuffer, StrBufferLen, uselen);
310 }
374ca955
A
311}
312
313UPerfFunction* StringPerformanceTest::TestStdLibAssign1()
314{
73c04bcf
A
315 if (line_mode) {
316 return new StringPerfFunction(StdLibAssign1, filelines_, numLines, uselen);
317 } else {
318 return new StringPerfFunction(StdLibAssign1, StrBuffer, StrBufferLen, uselen);
319 }
374ca955
A
320}
321
322UPerfFunction* StringPerformanceTest::TestStdLibAssign2()
323{
73c04bcf
A
324 if (line_mode) {
325 return new StringPerfFunction(StdLibAssign2, filelines_, numLines, uselen);
326 } else {
327 return new StringPerfFunction(StdLibAssign2, StrBuffer, StrBufferLen, uselen);
328 }
374ca955
A
329}
330
331UPerfFunction* StringPerformanceTest::TestStdLibGetch()
332{
73c04bcf
A
333 if (line_mode) {
334 return new StringPerfFunction(StdLibGetch, filelines_, numLines, uselen);
335 } else {
336 return new StringPerfFunction(StdLibGetch, StrBuffer, StrBufferLen, uselen);
337 }
374ca955
A
338}
339
340UPerfFunction* StringPerformanceTest::TestStdLibCatenate()
341{
73c04bcf
A
342 if (line_mode) {
343 return new StringPerfFunction(StdLibCatenate, filelines_, numLines, uselen);
344 } else {
345 //return new StringPerfFunction(StdLibCatenate, buffer, bufferLen, uselen);
346 return new StringPerfFunction(StdLibCatenate, StrBuffer, StrBufferLen, uselen);
347 }
374ca955
A
348}
349
350UPerfFunction* StringPerformanceTest::TestStdLibScan()
351{
73c04bcf
A
352 if (line_mode) {
353 return new StringPerfFunction(StdLibScan, filelines_, numLines, uselen);
354 } else {
355 return new StringPerfFunction(StdLibScan, StrBuffer, StrBufferLen, uselen);
356 }
374ca955
A
357}
358
359UPerfFunction* StringPerformanceTest::TestStdLibScan1()
360{
73c04bcf
A
361 if (line_mode) {
362 return new StringPerfFunction(StdLibScan1, filelines_, numLines, uselen);
363 } else {
364 return new StringPerfFunction(StdLibScan1, StrBuffer, StrBufferLen, uselen);
365 }
374ca955
A
366}
367
368UPerfFunction* StringPerformanceTest::TestStdLibScan2()
369{
73c04bcf
A
370 if (line_mode) {
371 return new StringPerfFunction(StdLibScan2, filelines_, numLines, uselen);
372 } else {
373 return new StringPerfFunction(StdLibScan2, StrBuffer, StrBufferLen, uselen);
374 }
375}
376
377