Back to index

lightning-sunbird  0.9+nobinonly
Classes | Functions | Variables
profile_main.cpp File Reference
#include "nscore.h"
#include <iostream.h>
#include <string>
#include <iomanip>
#include "nsInt64.h"
#include "prtime.h"
#include "nsString.h"

Go to the source code of this file.

Classes

class  TestTimer
class  Profiler

Functions

template<class T >
PRUint32 TotalLength (const T &s)
NS_SPECIALIZE_TEMPLATE PRUint32 TotalLength (const string &s)
template<class T >
PRUint32 Find (const T &text, const T &pattern)
NS_SPECIALIZE_TEMPLATE PRUint32 Find (const string &text, const string &pattern)
nsInt64 GetTime ()
int foo (const nsCString &)
static int test_construction ()
static int test_concat ()
static int test_concat_and_assign ()
static int test_compare ()
static int test_countchar ()
static int test_append_string ()
static int test_repeated_append_string ()
static int test_append_char ()
static int test_repeated_append_char ()
static int test_insert_string ()
static int test_find_string ()
int main ()
 main()

Variables

static const int kTestSucceeded = 0
static const int kTestFailed = 1
static const size_t N = 100000

Function Documentation

template<class T >
PRUint32 Find ( const T text,
const T pattern 
) [inline]

Definition at line 49 of file profile_main.cpp.

  {
    return text.Find(pattern);
  }

Here is the caller graph for this function:

NS_SPECIALIZE_TEMPLATE PRUint32 Find ( const string text,
const string pattern 
) [inline]

Definition at line 57 of file profile_main.cpp.

  {
    return text.find(pattern);
  }
int foo ( const nsCString ) [inline]

Definition at line 97 of file profile_main.cpp.

  {
    return 1;
  }
nsInt64 GetTime ( ) [inline]

Definition at line 64 of file profile_main.cpp.

  {
#ifdef XP_MAC
    UnsignedWide time;
    Microseconds(&time);
    return nsInt64( *reinterpret_cast<PRInt64*>(&time) );
#else
    return nsInt64( PR_Now() );
#endif
  }

Here is the call graph for this function:

Here is the caller graph for this function:

int main ( void  )

main()

Definition at line 458 of file profile_main.cpp.

  {

    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;
  }

Here is the call graph for this function:

static int test_append_char ( ) [static]

Definition at line 329 of file profile_main.cpp.

  {
    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;
  }

Here is the caller graph for this function:

static int test_append_string ( ) [static]

Definition at line 278 of file profile_main.cpp.

  {
    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;
  }

Here is the call graph for this function:

Here is the caller graph for this function:

static int test_compare ( ) [static]

Definition at line 228 of file profile_main.cpp.

  {
    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_concat ( ) [static]

Definition at line 146 of file profile_main.cpp.

  {
    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;
  }

Here is the call graph for this function:

Here is the caller graph for this function:

static int test_concat_and_assign ( ) [static]

Definition at line 194 of file profile_main.cpp.

  {
                //---------|---------|---------|---------|---------|---------|---------|
    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;
  }

Here is the call graph for this function:

Here is the caller graph for this function:

static int test_construction ( ) [static]

Definition at line 104 of file profile_main.cpp.

  {
    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;
  }

Here is the caller graph for this function:

static int test_countchar ( ) [static]

Definition at line 255 of file profile_main.cpp.

  {
    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;
  }

Here is the caller graph for this function:

static int test_find_string ( ) [static]

Definition at line 408 of file profile_main.cpp.

  {
    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;
  }

Here is the call graph for this function:

Here is the caller graph for this function:

static int test_insert_string ( ) [static]

Definition at line 385 of file profile_main.cpp.

  {
    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;
  }

Here is the call graph for this function:

Here is the caller graph for this function:

static int test_repeated_append_char ( ) [static]

Definition at line 362 of file profile_main.cpp.

  {
    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;
  }

Here is the call graph for this function:

Here is the caller graph for this function:

static int test_repeated_append_string ( ) [static]

Definition at line 302 of file profile_main.cpp.

  {
    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;
  }

Here is the call graph for this function:

Here is the caller graph for this function:

template<class T >
PRUint32 TotalLength ( const T s) [inline]

Definition at line 33 of file profile_main.cpp.

  {
    return s.Length();
  }

Here is the caller graph for this function:

Definition at line 41 of file profile_main.cpp.

  {
    return s.length();
  }

Variable Documentation

const int kTestFailed = 1 [static]

Definition at line 25 of file profile_main.cpp.

const int kTestSucceeded = 0 [static]

Definition at line 24 of file profile_main.cpp.

const size_t N = 100000 [static]

Definition at line 27 of file profile_main.cpp.