CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Jan 2005
    Location
    Akron, Ohio
    Posts
    670

    Comparing two functions for speed

    Hi folks,

    I'd like your opinion on which of the two functions are faster:

    Code:
    string& Index::Convert(string& word)
    {
         string::iterator it;
         it = &word[0];
         while(it != word.end())
         {
              if(*it == '_')
              {
                   *it = ' '; // converts '_' into a space.
              }
              ++it;
         }
         return word;
    }
    VERSUS:

    Code:
    string& Index::Convert(string& word)
    {
         double wordlength = word.length();
         double count = 0;
         for (double i = 0; i<=wordlength; i++)
         {
              if (word[i] == '_')
              {
                   word[i] = ' '; //converts a '_' into a space
              }
         }
         return word;
    }
    I'm inclined to believe the first is faster, since it only iterates through the string once, but I'm not sure.
    Last edited by paradoxresolved; August 30th, 2005 at 12:36 PM.

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Comparing two functions for speed

    Quote Originally Posted by paradoxresolved
    Hi folks,

    I'd like your opinion on which of the two functions are faster:
    Why an opinon? Why not see for yourself by writing a small app and test? The reason why opinions can be invalid is that the compiler can optimize what we are seeing, and the final code may not even be anything like the original.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: Comparing two functions for speed

    There is no reason why &word[0] should be assignable to std::string::iterator, but you might be lucky enough to get a compile error when it isn't. Use word.begin().

    By the way, why not test the replace function at the same time?

  4. #4
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725

    Re: Comparing two functions for speed

    Quote Originally Posted by paradoxresolved
    Hi folks,

    Code:
        string::iterator it;
         it = &word[0];
    &word[0] is not an iterator. It compiles on your implementation
    because string::iterator is probably a char* variable. Instead use:
    Code:
    string::iterator it = word.begin();
    You might consider using the replace algorithm ...

    Code:
    #include <algorithm>
    
    //
    
    std::replace(word.begin(),word.end(),'_',' ');

  5. #5
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Re: Comparing two functions for speed

    The second one is probably inefficient because it uses double where it should use string::size_type.
    And conversions from double to integer types are costy in CPU cycles.
    Last edited by SuperKoko; August 30th, 2005 at 12:50 PM.

  6. #6
    Join Date
    Aug 2000
    Location
    New Jersey
    Posts
    968

    Re: Comparing two functions for speed

    for (double i = 0; i<=wordlength; i++)
    Using <= in a for loop is rarely correct. In most cases this is an indication of logic error.
    That should be the following:
    for (double i = 0; i<wordlength; ++i)

    Also, to make a fair comparison, you should only call the end() method once in the first method.
    Example:
    Code:

    Code:
    string& Index::Convert(string& word)
    {
         string::iterator it= word.begin();
         string::iterator it_end= word.end();
         while(it != it_end)
         {
              if(*it == '_')
              {
                   *it = ' '; // converts '_' into a space.
              }
              ++it;
         }
         return word;
    }
    Most compilers are able to optimize iterators better then index operator's[], but more then likely, it will be hard for you to measure the difference.
    David Maisonave
    Author of Policy Based Synchronized Smart Pointer
    http://axter.com/smartptr


    Top ten member of C++ Expert Exchange.
    C++ Topic Area

  7. #7
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: Comparing two functions for speed

    Take a look at the following FAQs...

  8. #8
    Join Date
    Dec 2004
    Location
    Poland
    Posts
    1,165

    Re: Comparing two functions for speed

    Quote Originally Posted by paradoxresolved
    Hi folks,

    I'd like your opinion on which of the two functions are faster:
    My guess would be: none. Use replace algorithm instead (I did not find appropriate string::replace(...) member function, but maybe this version also exist, use it then).
    Creating program loops to operate on elements of STL containers instead of using container methods or algorithms when possible is hardly ever good idea (according to Scott Meyers and others).

    Hob
    B+!
    'There is no cat' - A. Einstein

    Use &#91;code] [/code] tags!

    Did YOU share your photo with us at CG Members photo gallery ?

  9. #9
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: Comparing two functions for speed

    Well there are 10 basic_string::replace functions in SGI, 12 in Dinkumware, a few in roguewave (couldn't count them). Axter's loop is likely to the be implementation of std::replace in most algorithms, albeit that it would be templated.

  10. #10
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: Comparing two functions for speed

    Quote Originally Posted by NMTop40
    Well there are 10 basic_string::replace functions in SGI...
    The STL replace functions have all the same purpose (generally a good idea to have functions with the same name doing the same thing ), which is to replace a substring with a different substring. Of course this can be used to implement the algorithm (see below), but I doubt that it will be the fastest way, as it treats the characters that ought to be replaced as substrings of size 1.
    Code:
    #include <string>
    using namespace std;
    
    string& convert( string& word )
    {
            string::size_type pos = word.find('_');
            while ( pos != string::npos )
            {
                    word.replace( pos, 1, 1, ' ' );
                    pos = word.find('_', pos);
            }
            return word;
    }

  11. #11
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: Comparing two functions for speed

    Quote Originally Posted by treuss
    The STL replace functions have all the same purpose (generally a good idea to have functions with the same name doing the same thing ), which is to replace a substring with a different substring...
    Sorry, I was stupid enough to check the string::replace functions, not the replace algorithm. Of course a simple
    Code:
    replace( word.begin(), word.end(), '_', ' ' );
    does the trick!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured