Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2012
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.58 KB | None | 0 0
  1. #include <assert.h>
  2. #include <iostream>
  3. #include <time.h>
  4. #include <string>
  5. #include <string.h>
  6. using namespace std;
  7.  
  8. namespace g {
  9.     bool wasCalled = false;
  10. }
  11.  
  12. extern void doNothing( char const* ) { g::wasCalled = true; }
  13.  
  14. class StopWatch
  15. {
  16. private:
  17.     clock_t     start_;
  18.     clock_t     end_;
  19.     bool        isRunning_;
  20. public:
  21.     void start()
  22.     {
  23.         assert( !isRunning_ );
  24.         start_ = clock();
  25.         end_ = 0;
  26.         isRunning_ = true;
  27.     }
  28.    
  29.     void stop()
  30.     {
  31.         if( isRunning_ )
  32.         {
  33.             end_ = clock();
  34.             isRunning_ = false;
  35.         }
  36.     }
  37.    
  38.     double seconds() const
  39.     {
  40.         return double( end_ - start_ )/CLOCKS_PER_SEC;
  41.     }
  42.    
  43.     StopWatch(): start_(), end_(), isRunning_() {}
  44. };
  45.  
  46. inline void testCStr( int const argc, char const* const argv0 )
  47. {
  48.     //  C-style character string implementation
  49.     //const char *pc = "a very long literal string";
  50.     const char *pc = (argc == 10000? argv0 : "a very long literal string");
  51.     //const size_t  len = strlen(pc +1);    //  space to allocate
  52.     const size_t  len = strlen(pc)+1;    //  space to allocate
  53.  
  54.     //  performance test on string allocation and copy
  55.     for (size_t ix = 0; ix != 1000000; ++ix) {
  56.         char *pc2 = new char[len + 1];  //  allocate the space
  57.         strcpy(pc2, pc);                //  do the copy
  58.         if (strcmp(pc2, pc) == 0)            //  use the new string
  59.             //;   //  do nothing
  60.             doNothing( pc2 );
  61.         delete [] pc2;                  //  free the memory
  62.     }
  63. }
  64.  
  65. inline void testCppStr( int const argc, char const* const argv0 )
  66. {
  67.     //  string implementation
  68.     //string str("a very long literal string");
  69.     string str( argc == 10000? argv0 : "a very long literal string" );
  70.  
  71.     //  performance test on string allocation and copy
  72.     for(int ix = 0; ix != 1000000; ++ix) {
  73.         string str2 = str;  //  do the copy, automatically allocated
  74.         if (str == str2)    //  use the new string
  75.             //;   //  do nothing
  76.             doNothing( &str2[0] );
  77.     }    //  str2 is automatically freed
  78. }
  79.        
  80. int main( int argc, char* argv[] )
  81. {
  82.     StopWatch   timer;
  83.    
  84.     timer.start();  testCStr( argc, argv[0] );  timer.stop();
  85.     cout << "C strings: " << timer.seconds() << " seconds." << endl;
  86.  
  87.     timer.start();  testCppStr( argc, argv[0] );  timer.stop();
  88.     cout << "C++ strings: " << timer.seconds() << " seconds." << endl;
  89.    
  90.     cout << boolalpha;
  91.     cout << "doNothing was called = " << g::wasCalled << "." << endl;
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement