Cross-post from SO-  Palindrome finding function I didn't know this site existed before now... awesome!

I just made a thread here:  https://stackoverflow.com/questions/8511620/c-palindrome-finder-optimization 

#include &lt;iostream&gt;
#include &lt;ostream&gt;
#include &lt;vector&gt;
#include &lt;fstream&gt;
#include &lt;algorithm&gt;
using namespace std;

bool isPal(string);

int main()
{
    vector&lt;string&gt; sVec;
    vector&lt;string&gt; sWords;
    vector&lt;string&gt; sTwoWords1;
    vector&lt;string&gt; sTwoWords2;
    char myfile[256]="/home/Damien/Test.txt";
    ifstream fin;
    string str;
    fin.open(myfile);
    if(!fin){
        cout &lt;&lt; "fin failed";
        return 0;
    } 
    while(fin){
        fin &gt;&gt; str;
        sWords.push_back(str);
        if(!fin){
            break;
        }
        if(isPal(str)){
            sVec.push_back(str);
        }
        else{
            getline(fin, str);
        }
    }
    reverse(sVec.begin(), sVec.end());
    for(int i =0; i &lt; sVec.size(); i++){ 
        cout &lt;&lt; sVec[i] &lt;&lt; " is a Palindrome " &lt;&lt;endl;
    } 

    // Test 2 
    for(int i=0; i&lt;sWords.size(); i++)
    {                                         
        for(int j=(i+1); j&lt;sWords.size(); j++){ 
            str = sWords[i]+sWords[j];
            if(isPal(str)){
                sTwoWords1.push_back(sWords[i]);
                sTwoWords2.push_back(sWords[j]);
            }
        }
    }
    fin.close();
    for(int i=0; i&lt;sTwoWords1.size();i++){ 
        cout &lt;&lt; sTwoWords1[i] &lt;&lt; " and " &lt;&lt; sTwoWords2[i] &lt;&lt; " are palindromic. \n";
    } 
    return 0;
}
bool isPal(string&amp; testing) {
    return std::equal(testing.begin(), testing.begin() + testing.size() / 2, testing.rbegin());
}


About optimizing some code, assuming that I take the revision to my isPal function, what else can I do to optimize the hell out of my code?

Thanks a lot for any / all recommendations!

Wordfile:  http://www.filedropper.com/ospd3
Reading a file:

while(fin){ 

    fin &gt;&gt; str; 
    sWords.push_back(str); 
    if(!fin){ 
        break; 
    } 


You push the value str before checking it worked. It should be:

while(fin)
{ 
    fin &gt;&gt; str; 
    if(!fin){ 
        break; 
    } 
    sWords.push_back(str); 


Or even better would be:

while(fin &gt;&gt; str)
{    
    sWords.push_back(str); 


Why are you doing this:

    else{
        getline(fin, str);
    }


Mixing operator >> and getline() makes the code hard to read.
Either use all operator >> or use getline() to get a line at a time then parse the line internally.

Note:: operator>> will ignore the '\n' at the end of the line and work. So if your file is a list of words one per line there is no need to use getline(). If on the other hand your file is a lot of text and you only want the first word on each line then you need to use getline().

Method 1: Use operator>>

 // If your input file has one word per line (or you want to use all words)
 while(file &gt;&gt; word)
 {
      // Do stuff
 }


Method 1: Use getline()

 // If your input file contains lots of words
 // But you only want to use the first word on each line
 while(std::getline(file, line))
 {
     std::stringstream  linestream(line);
     linestream &gt;&gt; word;

     // Do stuff
 }


You seem to reversing the line just to print it:

reverse(sVec.begin(), sVec.end());                          
for(int i =0; i &lt; sVec.size(); i++){                                
    cout &lt;&lt; sVec[i] &lt;&lt; " is a Palindrome " &lt;&lt;endl;                                  
}                                                                                           


Rather than reversing the array, just iterate it in reverse order:

for(std::vector&lt;std::string&gt;::const_iterator&amp; loop = sVec.rbegin(); loop != sVec.rend(); ++ loop)
{                               
    cout &lt;&lt; (*loop) &lt;&lt; " is a Palindrome " &lt;&lt;endl;                                  
}                                                                                           


Note on efficiency:

Using push_back() on an vector can be in-effecient if you are pushing a lot of data. As evertime the vector runs out of space it must allocate more memory and copy the string to the new buffer. You help by pre-allocting space.

sVec.reserve(1000); // Guess at the size
                    // Note when the vector runs out of space it will approximately
                    // double its internal buffer.