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.
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
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?
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(),'_',' ');
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.
Re: Comparing two functions for speed
Quote:
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.
Re: Comparing two functions for speed
Take a look at the following FAQs...
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
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.
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;
}
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!