1

I've written this usual fibonacci python code in python official idle;

def fib(n):
     a, b = 0, 1
     while a < n:
         print(a, end=' ')
         a, b = b, a+b
     print()
fib(10000000)

When i increased n,like i did in exemple, in idle it've taken around 30s-60s to finish it, after this,

I saved the python file as fibo.py and I ve opened it on cmd by importing module after opening python in cmd with -py extension.

Even if you put a very big number n in cmd, the process ends in less than 2 seconds always. Its a very very very high speed, but this isn't same for python idle,

Any answer would be appriciated.

aschipfl
  • 33,626
  • 12
  • 54
  • 99
  • 2
    IDLE probably handles I/O differently, making the `print`s slower. Try comparing the two with only a single print at the end of the function – Patrick Haugh Aug 01 '18 at 11:51
  • 2
    Try without the `print()` call inside the while loop. Input/output is often a bottle neck. – Håken Lid Aug 01 '18 at 11:52
  • 1
    Cmd also printing the numbers, but I agree with the idea that print slows the process of Python. Without prints they both are working pretty fast! Thanks for the comments. – Ali Öztürk Aug 01 '18 at 11:58
  • See the linked duplicate I'm pretty sure this is what you're looking for. – scharette Aug 01 '18 at 12:01
  • 1
    Actually its not dublicate because i’m askin why cmd is too fast here.. – Ali Öztürk Aug 01 '18 at 12:03
  • @AliÖztürk What do you mean by "too fast"? Too fast for what? Is it giving the wrong answer? – glibdud Aug 01 '18 at 12:07
  • its giving the right answer but while its taking 30s in idle its taking 2s in cmd, i mean the huge difference with “too fast” – Ali Öztürk Aug 01 '18 at 12:09
  • @glibdud too fast simply means it executes the same code, giving exactly the same results but using fewer seconds/microseconds – Onyambu Aug 01 '18 at 12:09
  • @AliÖztürk If it only takes 2 seconds to get the correct answer, then that's how long it should take. Thus, what you're actually asking is why Idle *doesn't* do it in two seconds, and I agree that the duplicate is the most likely reason why. – glibdud Aug 01 '18 at 12:11
  • @glibdud nope mate, i’m asking why Cmd is working that fast? You accomplish with same speed even if you hugely increase the number . That s what i was asking, it s written already, no? – Ali Öztürk Aug 01 '18 at 12:15
  • @AliÖztürk That has nothing to do with cmd or Idle; Fibonacci just isn't a very complex algorithm. A computer can do a *lot* of simple operations in two seconds. More time is probably being spent compiling the code than actually running it. – glibdud Aug 01 '18 at 12:18
  • @glibdud ok, so it’s because of the algorithm only you say? So there s no effect of being command prompt? – Ali Öztürk Aug 01 '18 at 12:21
  • 1
    @AliÖztürk Correct, the reason Idle isn't going as fast as cmd is because of the string handling. – glibdud Aug 01 '18 at 12:23
  • @glibdud Ok, thanks – Ali Öztürk Aug 01 '18 at 12:24
  • 1
    Idle executes your code in a separate process. Each print call generates a string that is encoded to bytes, sent through a socket, decoded back to a string, and then inserted into a GUI text widget that can contain a million short lines but slows down with lone lines. If you removed `end = ' '` I expect IDLE would run faster, whereas a Windows console would keep only the last 300 (default) to 9999 lines. – Terry Jan Reedy Aug 02 '18 at 01:20

0 Answers0