Click to See Complete Forum and Search --> : Comparing two functions for speed
paradoxresolved
August 30th, 2005, 12:31 PM
Hi folks,
I'd like your opinion on which of the two functions are faster:
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:
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.
Paul McKenzie
August 30th, 2005, 12:35 PM
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
NMTop40
August 30th, 2005, 12:44 PM
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?
Philip Nicoletti
August 30th, 2005, 12:45 PM
Hi folks,
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:
string::iterator it = word.begin();
You might consider using the replace algorithm ...
#include <algorithm>
//
std::replace(word.begin(),word.end(),'_',' ');
SuperKoko
August 30th, 2005, 12:46 PM
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.
Axter
August 30th, 2005, 03:12 PM
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:
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.
Andreas Masur
August 30th, 2005, 07:10 PM
Take a look at the following FAQs (http://www.codeguru.com/forum/showthread.php?p=1201805#profiling)...
Hobson
August 31st, 2005, 02:10 AM
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
NMTop40
August 31st, 2005, 04:14 AM
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.
treuss
August 31st, 2005, 10:51 AM
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.
#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;
}
treuss
August 31st, 2005, 10:58 AM
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 replace( word.begin(), word.end(), '_', ' ' ); does the trick!
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.