Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>In short</strong> - it's not slower, it's just stuck. </p> <p>The while loop in your python version is an infinite loop - your indentation doesn't include changing <code>j</code> so you'll never exit it. The fact that your program didn't just "take longer" but actually got completely stuck should have been a hint (don't feel bad though, I once waited 3 days before getting convinced about a similar scenario).</p> <p>That's one thing, fixing that would make the program stop, but with incorrect results - that's because the outer for loop is also missing the indentation - you want to run the check for each number in the range.</p> <p>Fixed code:</p> <pre><code>import time def iterative_brute_force(n): longest = 0 terms = 0 for i in range(1, n + 1): j = i this_terms = 1 while j != 1: this_terms += 1 if this_terms &gt; terms: terms = this_terms longest = i if j % 2 == 0: j = j / 2 else: j = 3 * j + 1 return longest, terms t0 = time.time() print(iterative_brute_force(10 ** 6)) t1 = time.time() total = t1-t0 print(total) </code></pre> <p>Gives - </p> <pre><code>(837799, 525) 34.4885718822 </code></pre> <p>while the c version gives -</p> <pre><code>longest: 837799 (525) It took 0.600000 seconds </code></pre> <p>There, now everything makes sense again, python is indeed slower and we can get to the real question :) </p> <p>On a side note though - this is far from being optimized, as you may repeat numbers you already visited. A better algorithm here would have been storing the results for these numbers in some handy lookup table.</p> <hr> <p>Now, regarding the fundamental question here (which is relevant even after fixing the code as you can see) - execution time across languages is a tricky domain, even if you truly perform the same algorithm in your code, the actual implementation is affected by compiler or interpreter behavior - Python is interpreted and therefore your code has to perform through another layer of code that manages your program, while C just runs natively. This opens up potential language features (and maybe some optimizations), and it would depend on benchmarking each application to see how well it works, but it's probably safe to say that on most workloads you'll observe that python (or other interpreted languages) behaves 10-100x slower due to this overhead. </p> <p>Furthermore - having compiled the c code in advance allows your compiler to produce a far better optimized code. It's possible to use JITting on python to mitigate that (and reduce the interpreter overhead a bit), but it's <a href="https://stackoverflow.com/questions/13034991/does-the-python-3-interpreter-have-a-jit-feature">not available</a> on all python implementations (at least not "pure" ones)</p> <p>See also the discussion at - <a href="https://stackoverflow.com/questions/3033329/why-are-python-programs-often-slower-than-the-equivalent-program-written-in-c-or">Why are Python Programs often slower than the Equivalent Program Written in C or C++?</a></p>
    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