Note that there are some explanatory texts on larger screens.

plurals
  1. POHow long is the running time of code in python
    text
    copied!<p>I was required to calculate approximately how long will it take to an avg computer to run this code, assuming the speed of the loop is linear (my teacher told me to assume it, I know it's not true, but we are beginners so..):</p> <pre><code>num = 2**100 cnt = 0 import time t0 = time.clock() for i in range(num): cnt = cnt+1 t1 = time.clock() print("running time: ", t1-t0, " sec") </code></pre> <p>I waited for 3 hours and didn't get anything, so, as people advised me to do, I calculated it for a smaller input: <code>2**20</code>. This took 0.24478681215692052 seconds.</p> <p>Now, I tried to continue the calculation in 2 ways that I think must get me the SAME result, but they aren't. </p> <ol> <li><p>if I assume the speed of the loop is linear, to calculate <code>2**100</code> I simply have to calculate <code>(0.24478681215692052)^5</code>, because <code>(2**20)**5 = 2**100</code>.</p> <p>the result I got was: 0.000878901251447358 seconds (which is really not logical)</p></li> <li><p>I tried another way: </p> <ul> <li><code>2**20</code> - 0.24478681215692052 sec</li> <li><code>2**100</code> - ? </li> </ul> <p>and multiply like this: </p> <pre><code>2**100 * 0.24478681215692052 / 2**20 </code></pre> <p>and got: 296101054897497890198453.29788130608283648 seconds by using <a href="http://keisan.casio.com/calculator" rel="nofollow">this calculator for big numbers</a>.</p></li> </ol> <p>how is it possible that I got 2 different answers? and neither seem really logical?</p>
 

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