Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ Palindrome finder optimization
    text
    copied!<p>I have been writing a palindrome finder in C++ and I have succeeded in writing one that is.... basic to say the least.</p> <p>I am looking simply to increase the runspeed of the program, right now it takes about ~1m 5s to run a test for palindromes / 2 word palindromes on a 1500 word wordlist using the functions that I have. I would like to try running it on a much larger file but fail to see where I can optimize further?</p> <p>Any help would be appreciated: P.S. This is not for school, just for leisure.</p> <pre><code>#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()); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload