CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Threaded View

  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.

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