|
-
August 30th, 2005, 12:31 PM
#1
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.
-
August 30th, 2005, 12:35 PM
#2
Re: Comparing two functions for speed
 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
-
August 30th, 2005, 12:44 PM
#3
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?
-
August 30th, 2005, 12:45 PM
#4
Re: Comparing two functions for speed
 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(),'_',' ');
-
August 30th, 2005, 12:46 PM
#5
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.
-
August 30th, 2005, 03:12 PM
#6
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.
-
August 30th, 2005, 07:10 PM
#7
Re: Comparing two functions for speed
Take a look at the following FAQs...
-
August 31st, 2005, 02:10 AM
#8
Re: Comparing two functions for speed
 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 [code] [/code] tags!
Did YOU share your photo with us at CG Members photo gallery ?
-
August 31st, 2005, 04:14 AM
#9
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.
-
August 31st, 2005, 10:51 AM
#10
Re: Comparing two functions for speed
 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;
}
-
August 31st, 2005, 10:58 AM
#11
Re: Comparing two functions for speed
 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|