Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>For 100000 elements, the Python code took 6.9 seconds while the C++ originally took above 37 seconds.</p> <p>I did some basic optimizations on your code and managed to get the C++ code above 100 times faster than the Python implementation. It now does 100000 elements in 0.06 seconds. That is 617 times faster than the original C++ code. </p> <p>The most important thing is to compile in Release mode, with all optimizations. This code is literally orders of magnitude slower in Debug mode.</p> <p>Next, I will explain the optimizations I did.</p> <ul> <li>Moved all vector declarations outside of the loop; replaced them by a clear() operation, which is much faster than calling the constructor.</li> <li>Replaced the call to pow(value, 2) by a multiplication : value * value.</li> <li>Instead of having a squares vector and calling sum on it, I sum the values in-place using just an integer.</li> <li>Avoided all string operations, which are very slow compared to integer operations. For instance, it is possible to compute the squares of each digit by repeatedly dividing by 10 and fetching the modulus 10 of the resulting value, instead of converting the value to a string and then each character back to int.</li> <li>Avoided all vector copies, first by replacing passing by value with passing by reference, and finally by eliminating the helper functions completely.</li> <li>Eliminated a few temporary variables.</li> <li>And probably many small details I forgot. Compare your code and mine side-by-side to see exactly what I did.</li> </ul> <p>It may be possible to optimize the code even more by using pre-allocated arrays instead of vectors, but this would be a bit more work and I'll leave it as an exercise to the reader. :P</p> <p>Here's the optimized code :</p> <pre><code>#include &lt;iostream&gt; #include &lt;vector&gt; #include &lt;string&gt; #include &lt;ctime&gt; #include &lt;algorithm&gt; #include &lt;windows.h&gt; using namespace std; void calcMain(int upperBound, vector&lt;int&gt;&amp; known); int main() { while(true) { vector&lt;int&gt; results; int upperBound; cout &lt;&lt; "Pick an upper bound: "; cin &gt;&gt; upperBound; long start, end; start = GetTickCount(); calcMain(upperBound, results); end = GetTickCount(); for (size_t i = 0; i &lt; results.size(); ++i) { cout &lt;&lt; results[i] &lt;&lt; ", "; } cout &lt;&lt; endl; double seconds = (double)(end-start) / 1000.0; cout &lt;&lt; seconds &lt;&lt; " seconds." &lt;&lt; endl &lt;&lt; endl; } return 0; } void calcMain(int upperBound, vector&lt;int&gt;&amp; known) { vector&lt;int&gt; history; for(int i = 0; i &lt;= upperBound; i++) { int current = i; history.clear(); while(true) { int temp = current; int sum = 0; while (temp &gt; 0) { sum += (temp % 10) * (temp % 10); temp /= 10; } current = sum; if(find(history.begin(), history.end(), current) != history.end()) { if(current == 1) { known.push_back(i); } break; } history.push_back(current); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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