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