VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/xpcom/tests/StringFactoringTests/profile_main.cpp@ 62330

Last change on this file since 62330 was 1, checked in by vboxsync, 55 years ago

import

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.9 KB
Line 
1// profile_main.cpp
2
3#include "nscore.h"
4#include <iostream.h>
5#include <string>
6#include <iomanip>
7
8#include "nsInt64.h"
9
10#ifdef XP_MAC
11#include <Timer.h>
12#include "Profiler.h"
13#else
14#include "prtime.h"
15#endif
16
17#ifndef TEST_STD_STRING
18#include "nsString.h"
19#else
20#include "nsStdStringWrapper.h"
21typedef nsStdCString nsCString;
22#endif
23
24static const int kTestSucceeded = 0;
25static const int kTestFailed = 1;
26
27static const size_t N = 100000;
28
29
30template <class T>
31inline
32PRUint32
33TotalLength( const T& s )
34 {
35 return s.Length();
36 }
37
38NS_SPECIALIZE_TEMPLATE
39inline
40PRUint32
41TotalLength( const string& s )
42 {
43 return s.length();
44 }
45
46template <class T>
47inline
48PRUint32
49Find( const T& text, const T& pattern )
50 {
51 return text.Find(pattern);
52 }
53
54NS_SPECIALIZE_TEMPLATE
55inline
56PRUint32
57Find( const string& text, const string& pattern )
58 {
59 return text.find(pattern);
60 }
61
62inline
63nsInt64
64GetTime()
65 {
66#ifdef XP_MAC
67 UnsignedWide time;
68 Microseconds(&time);
69 return nsInt64( *reinterpret_cast<PRInt64*>(&time) );
70#else
71 return nsInt64( PR_Now() );
72#endif
73 }
74
75class TestTimer
76 {
77 public:
78 TestTimer() : mStartTime(GetTime()) { }
79
80 ~TestTimer()
81 {
82 nsInt64 stopTime = GetTime();
83 nsInt64 totalTime = stopTime - mStartTime;
84#ifdef HAVE_LONG_LONG
85 cout << setw(10) << NS_STATIC_CAST(PRInt64, totalTime) << " µs : ";
86#else
87 cout << setw(10) << NS_STATIC_CAST(PRInt32, totalTime) << "µs : ";
88#endif
89 }
90
91 private:
92 nsInt64 mStartTime;
93 };
94
95inline
96int
97foo( const nsCString& )
98 {
99 return 1;
100 }
101
102static
103int
104test_construction()
105 {
106 cout << endl;
107
108 {
109 nsCString someCString;
110 int total = 0;
111 TestTimer timer;
112 for ( int i=0; i<N; ++i )
113 {
114 total += foo( someCString );
115 }
116 }
117 cout << "null loop time for constructor" << endl;
118
119
120 {
121 int total = 0;
122 TestTimer timer;
123 for ( int i=0; i<N; ++i )
124 {
125 total += foo( nsCString() );
126 }
127 }
128 cout << "nsCString()" << endl;
129
130
131 {
132 int total = 0;
133 TestTimer timer;
134 for ( int i=0; i<N; ++i )
135 {
136 total += foo( nsCString("This is a reasonable length string with some text in it and it is good.") );
137 }
138 }
139 cout << "nsCString(\"abc\")" << endl;
140
141 return kTestSucceeded;
142 }
143
144static
145int
146test_concat()
147 {
148 cout << endl;
149
150 //---------|---------|---------|---------|---------|---------|---------|
151 nsCString s1("This is a reasonable length string with some text in it and it is good.");
152 nsCString s2("This is another string that I will use in the concatenation test.");
153 nsCString s3("This is yet a third string that I will use in the concatenation test.");
154
155 PRUint32 len = TotalLength( s1 + s2 + s3 + s1 + s2 + s3 );
156 if ( len != (71 + 65 + 69 + 71 + 65 + 69) )
157 {
158 cout << "|test_concat()| FAILED" << endl;
159 return kTestFailed;
160 }
161
162
163 {
164 nsCString anEmptyCString;
165 TestTimer timer;
166 for ( int i=0; i<N; ++i )
167 {
168 len += TotalLength( anEmptyCString );
169 }
170 }
171 cout << "null loop time for concat" << endl;
172
173
174 {
175 TestTimer timer;
176 for ( int i=0; i<N; ++i )
177 len += TotalLength( s1 + s2 + s3 + s1 + s2 + s3 );
178 }
179 cout << "TotalLength( s1 + s2 + s3 + s1 + s2 + s3 )" << endl;
180
181
182 {
183 TestTimer timer;
184 for ( int i=0; i<N; ++i )
185 len += TotalLength( s1 + s2 );
186 }
187 cout << "TotalLength( s1 + s2 )" << endl;
188
189 return kTestSucceeded;
190 }
191
192static
193int
194test_concat_and_assign()
195 {
196 //---------|---------|---------|---------|---------|---------|---------|
197 nsCString s1("This is a reasonable length string with some text in it and it is good.");
198 nsCString s2("This is another string that I will use in the concatenation test.");
199 nsCString s3("This is yet a third string that I will use in the concatenation test.");
200
201 nsCString s4( s1 + s2 + s3 + s1 + s2 + s3 );
202 if ( TotalLength(s4) != (71 + 65 + 69 + 71 + 65 + 69) )
203 {
204 cout << "|test_concat()| FAILED" << endl;
205 return kTestFailed;
206 }
207
208
209 {
210 TestTimer timer;
211 for ( int i=0; i<N; ++i )
212 s4 = s1 + s2 + s3 + s1 + s2 + s3;
213 }
214 cout << "s4 = s1 + s2 + s3 + s1 + s2 + s3" << endl;
215
216 {
217 TestTimer timer;
218 for ( int i=0; i<N; ++i )
219 s4 = s1 + s2;
220 }
221 cout << "s4 = s1 + s2" << endl;
222
223 return kTestSucceeded;
224 }
225
226static
227int
228test_compare()
229 {
230 nsCString s1("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxThis is a reasonable length string with some text in it and it is good.");
231 nsCString s2("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxThis is a reasonable length string with some text in it and it is bad.");
232
233 int count = 0;
234 {
235 TestTimer timer;
236 for ( int i=0; i<N; ++i )
237 if ( s1 > s2 )
238 ++count;
239 }
240 cout << "s1 > s2" << endl;
241
242 {
243 TestTimer timer;
244 for ( int i=0; i<N; ++i )
245 if ( s1 == s1 )
246 ++count;
247 }
248 cout << "s1 == s1" << endl;
249
250 return kTestSucceeded;
251 }
252
253static
254int
255test_countchar()
256 {
257 nsCString s1("This is a reasonable length string with some text in it and it is good.");
258
259 if ( s1.CountChar('e') != 5 )
260 {
261 cout << "|test_countchar()| FAILED: found a count of " << s1.CountChar('e') << endl;
262 return kTestFailed;
263 }
264
265 PRUint32 total = 0;
266 {
267 TestTimer timer;
268 for ( int i=0; i<N; ++i )
269 total += s1.CountChar('e');
270 }
271 cout << "s1.CountChar('e')" << endl;
272
273 return kTestSucceeded;
274 }
275
276static
277int
278test_append_string()
279 {
280 nsCString s1("This is a reasonable length string with some text in it and it is good.");
281 nsCString s2("This is another string that I will use in the concatenation test.");
282
283 PRUint32 len = 0;
284
285 {
286 TestTimer timer;
287 for ( int i=0; i<N; ++i )
288 {
289 nsCString s3;
290 s3.Append(s1);
291 s3.Append(s2);
292 len += TotalLength(s3);
293 }
294 }
295 cout << "s3.Append(s1); s3.Append(s2)" << endl;
296
297 return kTestSucceeded;
298 }
299
300static
301int
302test_repeated_append_string()
303 {
304 nsCString s1("This is a reasonable length string with some text in it and it is good.");
305 nsCString s2("This is another string that I will use in the concatenation test.");
306
307 PRUint32 len = 0;
308
309 {
310 TestTimer timer;
311 for ( int i=0; i<N; ++i )
312 {
313 nsCString s3;
314 for ( int j=0; j<100; ++j )
315 {
316 s3.Append(s1);
317 s3.Append(s2);
318 len += TotalLength(s3);
319 }
320 }
321 }
322 cout << "repeated s3.Append(s1); s3.Append(s2)" << endl;
323
324 return kTestSucceeded;
325 }
326
327static
328int
329test_append_char()
330 {
331 cout << endl;
332
333 PRUint32 len = 0;
334
335 nsCString s1("hello");
336 PRUint32 oldLength = s1.Length();
337
338 {
339 TestTimer timer;
340 for ( int i=0; i<N; ++i )
341 {
342 s1.SetLength(oldLength);
343 }
344 }
345 cout << "null loop time for append char" << endl;
346
347 {
348 TestTimer timer;
349 for ( int i=0; i<N; ++i )
350 {
351 s1.Append('e');
352 s1.SetLength(oldLength);
353 }
354 }
355 cout << "s1.Append('e')" << endl;
356
357 return kTestSucceeded;
358 }
359
360static
361int
362test_repeated_append_char()
363 {
364 PRUint32 len = 0;
365
366 {
367 TestTimer timer;
368 for ( int i=0; i<N; ++i )
369 {
370 nsCString s1;
371 for ( int j=0; j<1000; ++j )
372 {
373 s1.Append('e');
374 len += TotalLength(s1);
375 }
376 }
377 }
378 cout << "repeated s1.Append('e')" << endl;
379
380 return kTestSucceeded;
381 }
382
383static
384int
385test_insert_string()
386 {
387 nsCString s1("This is a reasonable length string with some text in it and it is good.");
388
389 PRUint32 len = 0;
390
391 {
392 TestTimer timer;
393 for ( int i=0; i<N; ++i )
394 {
395 nsCString s2("This is another string that I will use in the concatenation test.");
396 s2.Insert(s1, 3);
397 len += TotalLength(s2);
398 }
399 }
400 cout << "s2.Insert(s1, 3)" << endl;
401
402 return kTestSucceeded;
403 }
404
405#ifndef TEST_STD_STRING
406static
407int
408test_find_string()
409 {
410 nsCString text("aaaaaaaaaab");
411 nsCString pattern("aab");
412
413 PRUint32 position = 0;
414 {
415 TestTimer timer;
416 for ( int i=0; i<N; ++i )
417 position = Find(text, pattern);
418 }
419 cout << "text.Find(pattern)" << endl;
420
421 return kTestSucceeded;
422 }
423#endif
424
425class Profiler
426 {
427 public:
428 Profiler()
429 {
430#if 0
431 ProfilerInit(collectDetailed, bestTimeBase, 100, 32);
432#endif
433 }
434
435 void
436 Dump( const char* output_name )
437 {
438 }
439
440 void
441 Dump( const unsigned char* output_name )
442 {
443#if 0
444 ProfilerDump(output_name);
445#endif
446 }
447
448 ~Profiler()
449 {
450#if 0
451 ProfilerDump(mOutputName);
452 ProfilerTerm();
453#endif
454 }
455 };
456
457int
458main()
459 {
460
461 cout << "String performance profiling. Compiled " __DATE__ " " __TIME__ << endl;
462#ifdef TEST_STD_STRING
463 cout << "Testing std::string." << endl;
464#else
465 cout << "Testing factored nsString." << endl;
466#endif
467
468 int tests_failed = 0;
469
470 Profiler profiler;
471
472 tests_failed += test_construction();
473 tests_failed += test_concat();
474 tests_failed += test_concat_and_assign();
475 tests_failed += test_compare();
476 tests_failed += test_countchar();
477 tests_failed += test_append_string();
478 tests_failed += test_repeated_append_string();
479 tests_failed += test_append_char();
480 tests_failed += test_repeated_append_char();
481 tests_failed += test_insert_string();
482#ifndef TEST_STD_STRING
483 tests_failed += test_find_string();
484#endif
485
486#ifdef TEST_STD_STRING
487 profiler.Dump("\pStandard String.prof");
488#else
489 profiler.Dump("\pFactored String.prof");
490#endif
491
492 if ( tests_failed )
493 cout << "One or more tests FAILED. Measurements may be invalid." << endl;
494
495 cout << "End of string performance profiling." << endl;
496 return 0;
497 }
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette